[BAEL-2998] - Adding example for @DirtiesContext
This commit is contained in:
parent
a76bf62420
commit
cc827908e0
@ -15,18 +15,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>
|
||||
|
@ -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,30 @@
|
||||
package com.baeldung.dirtiescontext;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.ToString;
|
||||
|
||||
@ToString
|
||||
@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,41 @@
|
||||
package com.baeldung.dirtiescontext;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
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.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest(classes = SpringDataRestApplication.class)
|
||||
class DirtiesContextTest {
|
||||
|
||||
@Autowired
|
||||
protected UserCache userCache;
|
||||
|
||||
@DirtiesContext(methodMode = MethodMode.AFTER_METHOD)
|
||||
@Test
|
||||
void testOne() {
|
||||
userCache.addUser("John Doe");
|
||||
userCache.printUserList("Test One");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testTwo() {
|
||||
userCache.printUserList("Test Two");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testThree() {
|
||||
userCache.addUser("Jane Doe");
|
||||
userCache.printUserList("Test Three");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFour() {
|
||||
userCache.printUserList("Test Four");
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user