Merge pull request #7857 from at508/master
[BAEL-2998] - Adding example for @DirtiesContext
This commit is contained in:
@@ -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;
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user