Merge pull request #7857 from at508/master
[BAEL-2998] - Adding example for @DirtiesContext
This commit is contained in:
commit
4d510c9b7d
testing-modules/spring-testing
@ -1,5 +1,6 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" 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">
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
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>
|
||||
<groupId>org.baeldung</groupId>
|
||||
<artifactId>spring-testing</artifactId>
|
||||
@ -15,18 +16,26 @@
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!-- test scoped -->
|
||||
<dependency>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>java-hamcrest</artifactId>
|
||||
<version>${hamcrest.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>${lombok.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
<version>LATEST</version>
|
||||
</dependency>
|
||||
|
||||
<!-- test scoped -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
@ -44,7 +53,7 @@
|
||||
<version>LATEST</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
@ -64,6 +73,17 @@
|
||||
<version>${junit.jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</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>
|
||||
<groupId>org.awaitility</groupId>
|
||||
<artifactId>awaitility</artifactId>
|
||||
|
13
testing-modules/spring-testing/src/main/java/com/baeldung/dirtiescontext/SpringDataRestApplication.java
Normal file
13
testing-modules/spring-testing/src/main/java/com/baeldung/dirtiescontext/SpringDataRestApplication.java
Normal file
@ -0,0 +1,13 @@
|
||||
package com.baeldung.dirtiescontext;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringDataRestApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringDataRestApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package com.baeldung.dirtiescontext;
|
||||
|
||||
public class User {
|
||||
|
||||
String firstName;
|
||||
|
||||
String lastName;
|
||||
|
||||
}
|
28
testing-modules/spring-testing/src/main/java/com/baeldung/dirtiescontext/UserCache.java
Normal file
28
testing-modules/spring-testing/src/main/java/com/baeldung/dirtiescontext/UserCache.java
Normal file
@ -0,0 +1,28 @@
|
||||
package com.baeldung.dirtiescontext;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
@Component
|
||||
public class UserCache {
|
||||
|
||||
@Getter
|
||||
private Set<String> userList = new HashSet<>();
|
||||
|
||||
public boolean addUser(String user) {
|
||||
return userList.add(user);
|
||||
}
|
||||
|
||||
public boolean removeUser(String user) {
|
||||
return userList.remove(user);
|
||||
}
|
||||
|
||||
public void printUserList(String message) {
|
||||
System.out.println(message + ": " + userList);
|
||||
}
|
||||
|
||||
}
|
49
testing-modules/spring-testing/src/test/java/com/baeldung/dirtiescontext/DirtiesContextIntegrationTest.java
Normal file
49
testing-modules/spring-testing/src/test/java/com/baeldung/dirtiescontext/DirtiesContextIntegrationTest.java
Normal file
@ -0,0 +1,49 @@
|
||||
package com.baeldung.dirtiescontext;
|
||||
|
||||
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.annotation.DirtiesContext.MethodMode;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
@TestMethodOrder(OrderAnnotation.class)
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@SpringBootTest(classes = SpringDataRestApplication.class)
|
||||
class DirtiesContextIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
protected UserCache userCache;
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void addJaneDoeAndPrintCache() {
|
||||
userCache.addUser("Jane Doe");
|
||||
userCache.printUserList("addJaneDoeAndPrintCache");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void printCache() {
|
||||
userCache.printUserList("printCache");
|
||||
}
|
||||
|
||||
@DirtiesContext(methodMode = MethodMode.AFTER_METHOD)
|
||||
@Test
|
||||
@Order(3)
|
||||
void addJohnDoeAndPrintCache() {
|
||||
userCache.addUser("John Doe");
|
||||
userCache.printUserList("addJohnDoeAndPrintCache");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(4)
|
||||
void printCacheAgain() {
|
||||
userCache.printUserList("printCacheAgain");
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user