From 9e34f40212a7ab9b3ae104c0518dfde04aea8105 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pelayo=20Rojas=20=C3=8D=C3=B1igo?= <31128562+Pelayori@users.noreply.github.com> Date: Mon, 1 Apr 2024 23:23:39 +0200 Subject: [PATCH] Unit tests (#162) * 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 --- .github/workflows/unit-tests-push.yml | 9 ++- pom.xml | 73 +++++++++++++++++++ .../services/InsertSampleDataService.java | 5 ++ src/main/resources/application.properties | 1 + src/test/java/com/uniovi/CucumberHooks.java | 17 +++++ ...nnerTest.java => CucumberRunnerTests.java} | 4 +- .../java/com/uniovi/CucumberWiqContext.java | 17 ----- .../java/com/uniovi/Wiq_IntegrationTests.java | 23 +++--- src/test/java/com/uniovi/Wiq_UnitTests.java | 21 ++++-- .../com/uniovi/steps/NavigateHomeStep.java | 22 +++++- .../com/uniovi/util/FirefoxWebDriver.java | 21 ------ .../java/com/uniovi/util/SeleniumUtils.java | 1 - src/test/resources/features/home_page.feature | 7 +- 13 files changed, 158 insertions(+), 63 deletions(-) create mode 100644 src/test/java/com/uniovi/CucumberHooks.java rename src/test/java/com/uniovi/{CucumberRunnerTest.java => CucumberRunnerTests.java} (68%) delete mode 100644 src/test/java/com/uniovi/CucumberWiqContext.java diff --git a/.github/workflows/unit-tests-push.yml b/.github/workflows/unit-tests-push.yml index 11deb24d..f64fe892 100644 --- a/.github/workflows/unit-tests-push.yml +++ b/.github/workflows/unit-tests-push.yml @@ -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 \ No newline at end of file diff --git a/pom.xml b/pom.xml index d02bd850..65f9bebe 100644 --- a/pom.xml +++ b/pom.xml @@ -15,6 +15,9 @@ wiq_es04b 17 + src/main/java,src/test/resources/features + ${project.basedir}/target/jacoco.exec + ${project.basedir}/target/site/jacoco/jacoco.xml @@ -33,6 +36,12 @@ org.springframework.boot spring-boot-starter-test test + + + org.junit.jupiter + junit-jupiter-engine + + org.projectlombok @@ -106,13 +115,77 @@ 7.14.0 test + + io.cucumber + cucumber-junit-platform-engine + 7.14.0 + test + + + org.jacoco + jacoco-maven-plugin + 0.8.11 + + + + prepare-agent + + + + report + test + + report + + + + jacoco-merge + verify + + merge + + + + + ${project.build.directory} + + *.exec + + + + ${project.build.directory}/jacoco-merged.exec + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.2.5 + + + **/Wiq_UnitTests.java + **/CucumberRunnerTests.java + + + org.springframework.boot spring-boot-maven-plugin + + + + + -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=* + + + + + diff --git a/src/main/java/com/uniovi/services/InsertSampleDataService.java b/src/main/java/com/uniovi/services/InsertSampleDataService.java index 9248c2fb..fce3766e 100644 --- a/src/main/java/com/uniovi/services/InsertSampleDataService.java +++ b/src/main/java/com/uniovi/services/InsertSampleDataService.java @@ -55,6 +55,11 @@ public void insertSampleQuestions() { return; } + generateSampleData(); + } + + @Transactional + public void generateSampleData() { if (!playerService.getUserByEmail("test@test.com").isPresent()) { PlayerDto player = new PlayerDto(); player.setEmail("test@test.com"); diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 6785d07b..c7dd0e75 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -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 diff --git a/src/test/java/com/uniovi/CucumberHooks.java b/src/test/java/com/uniovi/CucumberHooks.java new file mode 100644 index 00000000..359f9e82 --- /dev/null +++ b/src/test/java/com/uniovi/CucumberHooks.java @@ -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(); + } +} diff --git a/src/test/java/com/uniovi/CucumberRunnerTest.java b/src/test/java/com/uniovi/CucumberRunnerTests.java similarity index 68% rename from src/test/java/com/uniovi/CucumberRunnerTest.java rename to src/test/java/com/uniovi/CucumberRunnerTests.java index 3d9bcc15..328e229d 100644 --- a/src/test/java/com/uniovi/CucumberRunnerTest.java +++ b/src/test/java/com/uniovi/CucumberRunnerTests.java @@ -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 { } diff --git a/src/test/java/com/uniovi/CucumberWiqContext.java b/src/test/java/com/uniovi/CucumberWiqContext.java deleted file mode 100644 index 74b8d362..00000000 --- a/src/test/java/com/uniovi/CucumberWiqContext.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.uniovi; - -import com.uniovi.util.FirefoxWebDriver; -import io.cucumber.java.After; -import org.openqa.selenium.WebDriver; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Bean; - -public class CucumberWiqContext { - WebDriver driver; - - @Bean(name="webdriver", destroyMethod = "close") - public WebDriver webDriver() { - driver = new FirefoxWebDriver(); - return driver; - } -} diff --git a/src/test/java/com/uniovi/Wiq_IntegrationTests.java b/src/test/java/com/uniovi/Wiq_IntegrationTests.java index f7da1ced..2b265f0b 100644 --- a/src/test/java/com/uniovi/Wiq_IntegrationTests.java +++ b/src/test/java/com/uniovi/Wiq_IntegrationTests.java @@ -1,18 +1,12 @@ 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") @@ -20,10 +14,21 @@ @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; + } } diff --git a/src/test/java/com/uniovi/Wiq_UnitTests.java b/src/test/java/com/uniovi/Wiq_UnitTests.java index 69e128cc..f1003da9 100644 --- a/src/test/java/com/uniovi/Wiq_UnitTests.java +++ b/src/test/java/com/uniovi/Wiq_UnitTests.java @@ -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 players = playerService.getUsersByRole("ROLE_USER"); + Assertions.assertEquals(0, players.size()); } - } diff --git a/src/test/java/com/uniovi/steps/NavigateHomeStep.java b/src/test/java/com/uniovi/steps/NavigateHomeStep.java index acec65b0..f0652924 100644 --- a/src/test/java/com/uniovi/steps/NavigateHomeStep.java +++ b/src/test/java/com/uniovi/steps/NavigateHomeStep.java @@ -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 { @@ -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 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"); } } diff --git a/src/test/java/com/uniovi/util/FirefoxWebDriver.java b/src/test/java/com/uniovi/util/FirefoxWebDriver.java index f2139971..dc8d340e 100644 --- a/src/test/java/com/uniovi/util/FirefoxWebDriver.java +++ b/src/test/java/com/uniovi/util/FirefoxWebDriver.java @@ -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(); @@ -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(); - } } diff --git a/src/test/java/com/uniovi/util/SeleniumUtils.java b/src/test/java/com/uniovi/util/SeleniumUtils.java index 2ad04cbb..aee85f0c 100644 --- a/src/test/java/com/uniovi/util/SeleniumUtils.java +++ b/src/test/java/com/uniovi/util/SeleniumUtils.java @@ -101,7 +101,6 @@ static public List 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. diff --git a/src/test/resources/features/home_page.feature b/src/test/resources/features/home_page.feature index 986c0454..0c9f5a92 100644 --- a/src/test/resources/features/home_page.feature +++ b/src/test/resources/features/home_page.feature @@ -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" \ No newline at end of file + 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 \ No newline at end of file