[BAEL-2998] - ordering and renaming tests for @DirtiesContext example

This commit is contained in:
at508 2019-09-29 23:39:09 -04:00
parent e25f50fa1b
commit 4881c701fd
2 changed files with 36 additions and 19 deletions

View File

@ -1,5 +1,6 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>org.baeldung</groupId> <groupId>org.baeldung</groupId>
<artifactId>spring-testing</artifactId> <artifactId>spring-testing</artifactId>
@ -21,7 +22,7 @@
<version>${hamcrest.version}</version> <version>${hamcrest.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>
<version>${lombok.version}</version> <version>${lombok.version}</version>
@ -52,7 +53,7 @@
<version>LATEST</version> <version>LATEST</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId> <artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version> <version>${spring.version}</version>
</dependency> </dependency>
@ -72,6 +73,17 @@
<version>${junit.jupiter.version}</version> <version>${junit.jupiter.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>org.awaitility</groupId> <groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId> <artifactId>awaitility</artifactId>

View File

@ -1,17 +1,18 @@
package com.baeldung.dirtiescontext; package com.baeldung.dirtiescontext;
import org.junit.FixMethodOrder; import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith; import org.junit.jupiter.api.TestMethodOrder;
import org.junit.runners.MethodSorters; import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.DirtiesContext.MethodMode; import org.springframework.test.annotation.DirtiesContext.MethodMode;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit.jupiter.SpringExtension;
@FixMethodOrder(MethodSorters.NAME_ASCENDING) @TestMethodOrder(OrderAnnotation.class)
@RunWith(SpringJUnit4ClassRunner.class) @ExtendWith(SpringExtension.class)
@SpringBootTest(classes = SpringDataRestApplication.class) @SpringBootTest(classes = SpringDataRestApplication.class)
class DirtiesContextIntegrationTest { class DirtiesContextIntegrationTest {
@ -19,26 +20,30 @@ class DirtiesContextIntegrationTest {
protected UserCache userCache; protected UserCache userCache;
@Test @Test
void testA() { @Order(1)
void addJaneDoeAndPrintCache() {
userCache.addUser("Jane Doe"); userCache.addUser("Jane Doe");
userCache.printUserList("Test A"); userCache.printUserList("addJaneDoeAndPrintCache");
} }
@Test @Test
void testB() { @Order(2)
userCache.printUserList("Test B"); void printCache() {
userCache.printUserList("printCache");
} }
@DirtiesContext(methodMode = MethodMode.AFTER_METHOD) @DirtiesContext(methodMode = MethodMode.AFTER_METHOD)
@Test @Test
void testC() { @Order(3)
void addJohnDoeAndPrintCache() {
userCache.addUser("John Doe"); userCache.addUser("John Doe");
userCache.printUserList("Test C"); userCache.printUserList("addJohnDoeAndPrintCache");
} }
@Test @Test
void testD() { @Order(4)
userCache.printUserList("Test D"); void printCacheAgain() {
userCache.printUserList("printCacheAgain");
} }
} }