在集成测试过程中重置Spring-Boot [英] Reset Spring-Boot During Integration Tests

查看:112
本文介绍了在集成测试过程中重置Spring-Boot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想尝试使一个极端的案例在这里起作用.在我当前的项目中,大约有20个集成测试.一项新的集成测试需要@EnableAsync才能使测试工作:

I guess am trying to get a corner case to work here. In my current project there are about 20 integration tests. One new integration test requires @EnableAsync to make the test work:

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

单独运行时,此测试可以正常运行.

When run alone, this test works fine.

考虑到Maven和Eclipse在一个项目中执行测试,并且知道该环境仅创建一次并为所有集成测试重新使用(或软重置),因此一定要先运行此集成测试.但是,这几乎是(几乎?).

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. However, that's (nearly?) never the case.

因此,此集成测试(将近?)始终会失败.一种显而易见的解决方案是将@EnableAsync添加到所有集成测试中.但是,这是一个不好的依赖关系,我敢打赌,一旦有人添加了另一个集成测试并忘记了这一要求,我就会被打破.

Therefore, this integration test (nearly?) always fails. One obvious solution is to add @EnableAsync to all integration tests. However, that's a bad dependency which I bet is broken once somebody adds another integration test and forgets this requirement.

我正在寻找一种强制SpringRunner完全重置上下文并真正从头开始的方法,同时还要查看@EnableAsync.理想情况下,该方式包括在测试后也标记SpringRunner必须重置上下文(即删除@EnableAsync).这样,任何执行顺序都将确保只有一个测试具有@EnableAsync.

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. Ideally that way includes to flag that SpringRunner has to reset the context (i.e., remove the @EnableAsync) after the test, too. That way any order of execution would ensure that only that very one test has the @EnableAsync.

有没有办法做到这一点?还是可以通过@Before/@After方法中的代码手动打开/关闭异步填充?

Is there a way to do this? Or can I manually turn on/off the async-stuff by code in a @Before/@After method?

推荐答案

为什么,我只是偶然发现了……我现在所拥有的:

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...

这篇关于在集成测试过程中重置Spring-Boot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆