简体   繁体   English

在集成测试期间重置Spring-Boot

[英]Reset Spring-Boot During Integration Tests

I guess am trying to get a corner case to work here. 我想是想让一个极端的案例在这里工作。 In my current project there are about 20 integration tests. 在我当前的项目中,大约有20个集成测试。 One new integration test requires @EnableAsync to make the test work: 一种新的集成测试需要@EnableAsync来使测试工作:

@RunWith(SpringRunner.class)
@EnableAsync
@SpringBootTest(webEnvironment = WebEnvironment.NONE)
public class MyITest {
  :
}

When run alone, this test works fine. 单独运行时,此测试可以正常运行。

Considering Maven and Eclipse' execution of tests in one project and knowing that the environment is only created once and reused (or soft-reset) for all integration tests, it's somewhat a requirement that this integration test runs first. 考虑到Maven和Eclipse在一个项目中执行测试,并且知道该环境仅创建一次并为所有集成测试重新使用(或软重置),因此一定要先运行此集成测试。 However, that's (nearly?) never the case. 但是,(几乎?)从来没有这样。

Therefore, this integration test (nearly?) always fails. 因此,此集成测试(几乎?)总是失败。 One obvious solution is to add @EnableAsync to all integration tests. 一种显而易见的解决方案是将@EnableAsync添加到所有集成测试中。 However, that's a bad dependency which I bet is broken once somebody adds another integration test and forgets this requirement. 但是,这是一个不好的依赖关系,我敢打赌,一旦有人添加了另一个集成测试并忘记了这一要求,我就会打破这种依赖关系。

I'm looking for a way to force the SpringRunner to completely reset the context and really start it from scratch also looking at @EnableAsync . 我正在寻找一种方法来强制SpringRunner完全重置上下文,并通过查看@EnableAsync真正从头开始。 Ideally that way includes to flag that SpringRunner has to reset the context (ie, remove the @EnableAsync ) after the test, too. 理想情况下,该方式还包括在测试之后标记SpringRunner也必须重置上下文(即,删除@EnableAsync )。 That way any order of execution would ensure that only that very one test has the @EnableAsync . 这样,任何执行顺序都将确保只有一个测试具有@EnableAsync

Is there a way to do this? 有没有办法做到这一点? Or can I manually turn on/off the async-stuff by code in a @Before / @After method? 还是可以通过@Before / @After方法中的代码手动打开/关闭异步材料?

take a look at DirtiesContext 看看DirtiesContext

Not sure if this is what you're looking for. 不知道这是您要找的东西。

Possible duplicate of: How do you reset Spring JUnit application context after a test class dirties it? 可能的重复: 测试类弄脏后如何重置Spring JUnit应用程序上下文?

Whow, I think I just found out by accident... What I have now: 谁啊,我想我是偶然发现的……我现在拥有的是:

@RunWith(SpringRunner.class)
@EnableAsync
@SpringBootTest(webEnvironment = WebEnvironment.NONE, classes = {
    ClassWithAnAutowiredAsyncDependency.class // <=== difference!!! ===>
})
public class MyITest {
:
  @Autowired
  private ClassWithAnAutowiredAsyncDependency mine;
:
}

It seems as if the given classes are reset (specially?) or at least the autowiring happens in there again or something. 似乎是给定的类被重置(特别是?),或者至少自动装配又在那儿发生了。 I can't explain it any different. 我无法解释任何不同。

I'm sure that this integration test is not the first integration test being run and still the asynchronous bit seems to be in place. 我确定该集成测试不是正在运行的第一个集成测试,并且异步位似乎仍然存在。

Well, test is green, it works... 好吧,测试是绿色的,它可以工作...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM