我有一个单元测试,可以为 JUnit 测试类实例化一次应用程序:
@RunWith(SpringRunner.class)
@SpringBootTest(properties = "server.port=9000",
classes = Application.class,
webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class TodoWebDriverTest {
// many test methods
}
但我希望每个测试方法拆除并实例化应用程序(全部套接字监听器)。我目前依赖于一些注释(见上文),但很乐意放弃它们以获得可靠的纯 Java 启动/启动功能:
app = SpringSomething.prepare(Appication.class, other params);
app.start();
waitForProofOfBeingStarted(app);
在这里,我 fork 了别人的应用程序 - https://github.com/paul-hammant/todo-backend-spring4-java8 - 并添加了线路模拟(没有服务虚拟化框架)和三个 WebDriver 测试。总而言之,构建大约需要 30 秒。 这显示了每个测试类一次的应用程序设置,并且是一个更大的解决方案,它将在管道中显示多个 Junit 测试阶段,但我需要将其翻转为每次一次-测试方法应用程序设置。
最佳答案
您想要 @DirtiesContext
annotation
@RunWith(SpringRunner.class)
@SpringBootTest(properties = "server.port=9000",
classes = Application.class,
webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@DirtiesContext(classMode = BEFORE_EACH_TEST_METHOD)
public class TodoWebDriverTest {
// many test methods
}
关于java - 如何使用 Junit 配置 Spring Boot 的按测试方法实例化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42270081/