generated from Arquisoft/wiq_0
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #158 from Arquisoft/unit-tests
Add test framework
- Loading branch information
Showing
8 changed files
with
288 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
name: Application tests | ||
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
- unit-tests | ||
- develop | ||
pull_request: | ||
types: [opened, synchronize, reopened] | ||
|
||
jobs: | ||
app-tests-analyze: | ||
runs-on: ubuntu-latest | ||
services: | ||
mysql: | ||
image: mysql:latest | ||
env: | ||
MYSQL_ROOT_PASSWORD: root | ||
MYSQL_DATABASE: test_database | ||
ports: | ||
- 3306:3306 | ||
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3 | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
|
||
- name: Cache Maven packages | ||
uses: actions/cache@v2 | ||
with: | ||
path: | | ||
~/.m2 | ||
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} | ||
restore-keys: | | ||
${{ runner.os }}-m2 | ||
- name: Set up JDK 17 | ||
uses: actions/setup-java@v1 | ||
with: | ||
java-version: '17' | ||
|
||
- name: Add exec permission to mvnw | ||
run: chmod +x mvnw | ||
|
||
- name: Compile the application | ||
run: ./mvnw -B clean install -DskipTests=true | ||
|
||
- name: Start the application | ||
run: ./mvnw spring-boot:run & | ||
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: 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 | ||
env: | ||
SPRING_PROFILES_ACTIVE: test | ||
headless: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.context.event.ApplicationReadyEvent; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.time.LocalDateTime; | ||
|
@@ -31,22 +32,29 @@ public class InsertSampleDataService { | |
private final CategoryService categoryService; | ||
private final QuestionRepository questionRepository; | ||
private final GameSessionRepository gameSessionRepository; | ||
private Environment environment; | ||
|
||
private Logger log = LoggerFactory.getLogger(InsertSampleDataService.class);; | ||
|
||
public InsertSampleDataService(PlayerService playerService, QuestionService questionService, | ||
CategoryService categoryService, QuestionRepository questionRepository, | ||
GameSessionRepository gameSessionRepository) { | ||
GameSessionRepository gameSessionRepository, Environment environment) { | ||
this.playerService = playerService; | ||
this.questionService = questionService; | ||
this.categoryService = categoryService; | ||
this.questionRepository = questionRepository; | ||
this.gameSessionRepository = gameSessionRepository; | ||
this.environment = environment; | ||
} | ||
|
||
@Transactional | ||
@EventListener(ApplicationReadyEvent.class) // Uncomment this line to insert sample data on startup | ||
public void insertSampleQuestions() { | ||
if (Arrays.stream(environment.getActiveProfiles()).anyMatch(env -> (env.equalsIgnoreCase("test")))) { | ||
log.info("Test profile active, skipping sample data insertion"); | ||
return; | ||
} | ||
|
||
if (!playerService.getUserByEmail("[email protected]").isPresent()) { | ||
PlayerDto player = new PlayerDto(); | ||
player.setEmail("[email protected]"); | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.uniovi; | ||
|
||
import io.github.bonigarcia.wdm.WebDriverManager; | ||
import org.junit.jupiter.api.*; | ||
import org.openqa.selenium.WebDriver; | ||
import org.openqa.selenium.chrome.ChromeDriver; | ||
import org.openqa.selenium.firefox.FirefoxDriver; | ||
import org.openqa.selenium.firefox.FirefoxOptions; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.test.annotation.DirtiesContext; | ||
import org.springframework.test.context.ActiveProfiles; | ||
|
||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
@SpringBootTest | ||
@Tag("integration") | ||
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD) | ||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class) | ||
@ActiveProfiles("test") | ||
class Wiq_IntegrationTests { | ||
static final String URL = "http://localhost:3000/"; | ||
|
||
static WebDriver driver; | ||
|
||
@Autowired | ||
Environment env; | ||
|
||
@BeforeEach | ||
public void begin() { | ||
if (driver == null) { | ||
WebDriverManager.firefoxdriver().setup(); | ||
if (env.getProperty("headless") != null && env.getProperty("headless").equals("true")) { | ||
FirefoxOptions options = new FirefoxOptions(); | ||
options.addArguments("--headless"); | ||
driver = new FirefoxDriver(options); | ||
} else { | ||
driver = new FirefoxDriver(); | ||
} | ||
} | ||
driver.navigate().to(URL); | ||
} | ||
|
||
@AfterEach | ||
void tearDown() { | ||
driver.manage().deleteAllCookies(); | ||
} | ||
|
||
@AfterAll | ||
public static void end() { | ||
driver.quit(); | ||
} | ||
|
||
@Test | ||
@Order(1) | ||
void testHome() { | ||
// Check the title | ||
Assertions.assertEquals("Wikigame", driver.getTitle()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
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 org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.annotation.DirtiesContext; | ||
import org.springframework.test.context.ActiveProfiles; | ||
|
||
@SpringBootTest | ||
@Tag("unit") | ||
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD) | ||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class) | ||
@ActiveProfiles("test") | ||
class Wiq_UnitTests { | ||
|
||
@Test | ||
void contextLoads() { | ||
} | ||
|
||
} |
Oops, something went wrong.