Skip to content

Commit

Permalink
Unit tests (#163)
Browse files Browse the repository at this point in the history
* Test coverage fix

* Test coverage fix

* Test coverage fix

* Test coverage fix

* Test coverage fix

* Test coverage fix

* Test coverage fix

* Test coverage fix

* Test coverage fix

* Fix multiple cucumber scenarios. Persistent webdriver session

* Test run integration tests

* Test run integration tests

* Test run integration tests

* Test run integration tests

* Test run integration tests

* Test run integration tests

* Test run integration tests

* Test run integration tests

* Test run integration tests

* Test run integration tests

* Test run integration tests

* Test run integration tests

* Test run integration tests

* Test run integration tests

* Test run integration tests

* Test run integration tests

* Test run integration tests

* Test run integration tests

* Test run integration tests

* Test run integration tests

* Test run integration tests

* Test run integration tests

* Add caching back

* Add caching back

* Add caching back

* Add caching back

* Add caching back

* Add caching back

* Add caching back

* Add caching back

* Add caching back

* Add caching back

* Add caching back

* Fix some tests

* Fix some tests

* Fix some tests
  • Loading branch information
Pelayori authored Apr 1, 2024
1 parent 9e34f40 commit ce51616
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/unit-tests-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ jobs:
run: ss -tuln
- name: Run all tests with sonar analysis
run: |
./mvnw -B org.jacoco:jacoco-maven-plugin:prepare-agent clean test sonar:sonar -Dsonar.projectKey=Arquisoft_wiq_es04b -Dsonar.organization=arquisoft -Dsonar.branch.name=${{ github.ref }} -Dsonar.host.url=https://sonarcloud.io -Dsonar.login=${{ secrets.SONAR_TOKEN }} -Dspring.profiles.active=test -Dspring.datasource.url=jdbc:mysql://localhost:3306/test_database -Dspring.datasource.username=root -Dspring.datasource.password=root -Dspring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver -Dtest="com.uniovi.Wiq_UnitTests,com.uniovi.CucumberRunnerTests"
./mvnw -B org.jacoco:jacoco-maven-plugin:prepare-agent clean test sonar:sonar -DEXCLUDE_JUNIT=true -Dsonar.projectKey=Arquisoft_wiq_es04b -Dsonar.organization=arquisoft -Dsonar.branch.name=${{ github.ref }} -Dsonar.host.url=https://sonarcloud.io -Dsonar.login=${{ secrets.SONAR_TOKEN }} -Dspring.profiles.active=test -Dspring.datasource.url=jdbc:mysql://localhost:3306/test_database -Dspring.datasource.username=root -Dspring.datasource.password=root -Dspring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver -Dtest="com.uniovi.Wiq_UnitTests,com.uniovi.CucumberRunnerTests"
env:
SPRING_PROFILES_ACTIVE: test
headless: true
54 changes: 43 additions & 11 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand Down Expand Up @@ -189,5 +178,48 @@
</plugin>
</plugins>
</build>
<profiles>
<profile>
<!-- This profile is activated when the EXCLUDE_JUNIT environment variable is set to true -->
<id>exclude-junit</id>
<activation>
<property>
<name>env.EXCLUDE_JUNIT</name>
<value>true</value>
</property>
</activation>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</profile>

<profile>
<!-- This profile is active by default when the EXCLUDE_JUNIT environment variable is not set -->
<id>include-junit</id>
<activation>
<activeByDefault>true</activeByDefault>
<property>
<name>!env.EXCLUDE_JUNIT</name>
</property>
</activation>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<!-- No exclusions -->
</dependency>
</dependencies>
</profile>
</profiles>
</project>
2 changes: 1 addition & 1 deletion src/main/java/com/uniovi/entities/Role.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class Role {
@Id
private String name;

@ManyToMany(mappedBy = "roles")
@ManyToMany(mappedBy = "roles", fetch = FetchType.EAGER)
private Set<Player> players = new HashSet<>();

public Role(String name) {
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/com/uniovi/services/InsertSampleDataService.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ public InsertSampleDataService(PlayerService playerService, QuestionService ques
@Transactional
@EventListener(ApplicationReadyEvent.class) // Uncomment this line to insert sample data on startup
public void insertSampleQuestions() {
if (!playerService.getUserByEmail("[email protected]").isPresent()) {
PlayerDto player = new PlayerDto();
player.setEmail("[email protected]");
player.setUsername("test");
player.setPassword("test");
player.setRoles(new String[]{"ROLE_USER"});
playerService.generateApiKey(playerService.addNewPlayer(player));
}

if (Arrays.stream(environment.getActiveProfiles()).anyMatch(env -> (env.equalsIgnoreCase("test")))) {
log.info("Test profile active, skipping sample data insertion");
return;
Expand All @@ -60,14 +69,6 @@ public void insertSampleQuestions() {

@Transactional
public void generateSampleData() {
if (!playerService.getUserByEmail("[email protected]").isPresent()) {
PlayerDto player = new PlayerDto();
player.setEmail("[email protected]");
player.setUsername("test");
player.setPassword("test");
player.setRoles(new String[]{"ROLE_USER"});
playerService.generateApiKey(playerService.addNewPlayer(player));
}

questionRepository.deleteAll();

Expand Down
4 changes: 1 addition & 3 deletions src/test/java/com/uniovi/Wiq_UnitTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import org.springframework.test.context.ActiveProfiles;
import java.util.List;

import java.util.Optional;

@SpringBootTest
@Tag("unit")
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD)
Expand All @@ -24,6 +22,6 @@ public class Wiq_UnitTests {
@Order(1)
public void testPlayerService() {
List<Player> players = playerService.getUsersByRole("ROLE_USER");
Assertions.assertEquals(0, players.size());
Assertions.assertEquals(1, players.size());
}
}

0 comments on commit ce51616

Please sign in to comment.