Skip to content

Commit

Permalink
Unit tests (#162)
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
  • Loading branch information
Pelayori authored Apr 1, 2024
1 parent 36a10aa commit 9e34f40
Show file tree
Hide file tree
Showing 13 changed files with 158 additions and 63 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/unit-tests-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,19 @@ jobs:
run: ./mvnw -B clean install -DskipTests=true

- name: Start the application
run: ./mvnw spring-boot:run &
run: |
./mvnw spring-boot:run &
sleep 15
env:
SPRING_DATASOURCE_URL: jdbc:mysql://localhost:3306/test_database
SPRING_DATASOURCE_USERNAME: root
SPRING_DATASOURCE_PASSWORD: root
SPRING_DATASOURCE_DRIVER_CLASS_NAME: com.mysql.cj.jdbc.Driver

- name: Check listening ports
run: ss -tuln
- name: Run all tests with sonar analysis
run: |
./mvnw -B verify 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
./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"
env:
SPRING_PROFILES_ACTIVE: test
headless: true
73 changes: 73 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
<description>wiq_es04b</description>
<properties>
<java.version>17</java.version>
<sonar.sources>src/main/java,src/test/resources/features</sonar.sources>
<sonar.jacoco.reportPaths>${project.basedir}/target/jacoco.exec</sonar.jacoco.reportPaths>
<sonar.coverage.jacoco.xmlReportPaths>${project.basedir}/target/site/jacoco/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>
</properties>
<dependencies>
<dependency>
Expand All @@ -33,6 +36,12 @@
<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>
Expand Down Expand Up @@ -106,13 +115,77 @@
<version>7.14.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit-platform-engine</artifactId>
<version>7.14.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.11</version> <!-- Use the latest version available -->
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>jacoco-merge</id>
<phase>verify</phase>
<goals>
<goal>merge</goal>
</goals>
<configuration>
<fileSets>
<fileSet>
<directory>${project.build.directory}</directory>
<includes>
<include>*.exec</include>
</includes>
</fileSet>
</fileSets>
<destFile>${project.build.directory}/jacoco-merged.exec</destFile>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
<configuration>
<includes>
<include>**/Wiq_UnitTests.java</include>
<include>**/CucumberRunnerTests.java</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<configuration>
<jvmArguments>
-javaagent:${settings.localRepository}/org/jacoco/org.jacoco.agent/0.8.11/org.jacoco.agent-0.8.11-runtime.jar=output=tcpserver,address=*,port=6300,includes=*
</jvmArguments>
</configuration>
</execution>
<!-- Define stop goal execution as well -->
</executions>
</plugin>
</plugins>
</build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public void insertSampleQuestions() {
return;
}

generateSampleData();
}

@Transactional
public void generateSampleData() {
if (!playerService.getUserByEmail("[email protected]").isPresent()) {
PlayerDto player = new PlayerDto();
player.setEmail("[email protected]");
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Port 3000 for testing, local deployment
server.port=3000
server.address=0.0.0.0

# HSQL db
spring.datasource.driver-class-name=org.hsqldb.jdbc.JDBCDriver
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/com/uniovi/CucumberHooks.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.uniovi;

import io.cucumber.java.After;
import io.cucumber.java.AfterAll;
import io.cucumber.java.Scenario;

public class CucumberHooks extends Wiq_IntegrationTests {
@After
public void cleanUpAfterScenario(Scenario scenario) {
driver.manage().deleteAllCookies();
}

@AfterAll
public static void before_or_after_all() {
driver.quit();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;

@RunWith(Cucumber.class)
@CucumberOptions(plugin = {"pretty"}, features = "src/test/resources/features")
public class CucumberRunnerTest {
@SpringBootTest
public class CucumberRunnerTests {
}
17 changes: 0 additions & 17 deletions src/test/java/com/uniovi/CucumberWiqContext.java

This file was deleted.

23 changes: 14 additions & 9 deletions src/test/java/com/uniovi/Wiq_IntegrationTests.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
package com.uniovi;

import com.uniovi.util.FirefoxWebDriver;
import io.cucumber.java.After;
import io.cucumber.spring.CucumberContextConfiguration;
import org.junit.jupiter.api.*;
import org.openqa.selenium.WebDriver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;

import static org.junit.jupiter.api.Assertions.fail;

@SpringBootTest
@Tag("integration")
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@ActiveProfiles("test")
@CucumberContextConfiguration
@ContextConfiguration(classes = CucumberWiqContext.class)
public class Wiq_IntegrationTests {
protected static final String URL = "http://localhost:3000/";

@Autowired
protected WebDriver driver;
protected static WebDriver driver;

public Wiq_IntegrationTests() {
driver = webDriver();
}

public WebDriver webDriver() {
if (driver != null) {
return driver;
}

driver = new FirefoxWebDriver();
return driver;
}
}
21 changes: 14 additions & 7 deletions src/test/java/com/uniovi/Wiq_UnitTests.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
package com.uniovi;

import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import com.uniovi.entities.Player;
import com.uniovi.services.PlayerService;
import org.junit.jupiter.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
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)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@ActiveProfiles("test")
class Wiq_UnitTests {
public class Wiq_UnitTests {
@Autowired
private PlayerService playerService;

@Test
void contextLoads() {
@Order(1)
public void testPlayerService() {
List<Player> players = playerService.getUsersByRole("ROLE_USER");
Assertions.assertEquals(0, players.size());
}

}
22 changes: 19 additions & 3 deletions src/test/java/com/uniovi/steps/NavigateHomeStep.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
package com.uniovi.steps;

import com.uniovi.*;
import com.uniovi.util.SeleniumUtils;
import io.cucumber.java.After;
import io.cucumber.java.AfterAll;
import io.cucumber.java.Scenario;
import io.cucumber.java.en.And;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import org.junit.jupiter.api.Assertions;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import java.util.List;

public class NavigateHomeStep extends Wiq_IntegrationTests {

Expand All @@ -18,8 +27,15 @@ public void i_should_see_the_title(String title) {
Assertions.assertEquals(title, driver.getTitle());
}

@After
public void cleanUpAfterScenario() {
driver.manage().deleteAllCookies();
@When("I click the register button")
public void i_click_the_register_button() {
List<WebElement> elems = SeleniumUtils.waitLoadElementsBy(driver, "@href", "signup", 5);
elems.get(0).click();
}

@Then("I should see the register page")
public void i_should_see_the_register_page() {
SeleniumUtils.waitLoadElementsBy(driver, "h2", "Regístrate", 5);
SeleniumUtils.textIsPresentOnPage(driver, "Regístrate");
}
}
21 changes: 0 additions & 21 deletions src/test/java/com/uniovi/util/FirefoxWebDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,9 @@
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.support.events.EventFiringWebDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FirefoxWebDriver extends EventFiringWebDriver {

private static final WebDriver webdriver;
private static final Logger logger = LoggerFactory.getLogger(FirefoxWebDriver.class);

private static final Thread CLOSE_THREAD = new Thread() {
@Override
public void run() {
webdriver.close();
}
};

static {
WebDriverManager.firefoxdriver().setup();
Expand All @@ -29,19 +18,9 @@ public void run() {
} else {
webdriver = new FirefoxDriver();
}

Runtime.getRuntime().addShutdownHook(CLOSE_THREAD);
}

public FirefoxWebDriver() {
super(webdriver);
}

@Override
public void close() {
if (Thread.currentThread() != CLOSE_THREAD) {
logger.info("You shouldn't close this WebDriver. It's shared and will close when the JVM exits.");
}
super.close();
}
}
1 change: 0 additions & 1 deletion src/test/java/com/uniovi/util/SeleniumUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ static public List<WebElement> waitLoadElementsBy(WebDriver driver, String crite


/**
* PROHIBIDO USARLO PARA VERSIÓN FINAL.
* Esperar "segundos" durante la ejecucion del navegador
* @param driver: apuntando al navegador abierto actualmente.
* @param seconds: Segundos de bloqueo de la ejecución en el navegador.
Expand Down
7 changes: 6 additions & 1 deletion src/test/resources/features/home_page.feature
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@ Feature: I enter the webpage

Scenario: I see the title
Given I am in the home page
Then I should see the title "Wikigame"
Then I should see the title "Wikigame"

Scenario: I click register
Given I am in the home page
When I click the register button
Then I should see the register page

0 comments on commit 9e34f40

Please sign in to comment.