我有多个@WebMvcTest
- 带注释的测试类,如果单独执行(例如通过mvn -Dtest=BTest test
或通过 IDE)。
但是,如果它们一起执行(例如通过mvn test
or mvn package
)一项测试失败(BTest
在下面的代码中)因为@SpyBean
- 带注释的字段不会拦截方法调用。其他测试都没有@SpyBean
's.
对应的代码结构如下:
// ATest.java
@WebMvcTest
class ATest {
...
}
// BTest.java
@WebMvcTest
class BTest {
...
@SpyBean
private ComponentToSpyOn comp;
...
@Test
void testSomething() {
...
latch = new CountDownLatch(1);
Mockito.doAnswer(inv -> {
var result = inv.callRealMethod();
latch.countDown();
return result;
}).when(comp).execute(any());
// Execute something that would lead to call interception
// Wait for method return
if (!latch.await(3000, TimeUnit.MILLISECONDS)) {
latch = null;
throw new TimeoutException();
}
...
}
}
// CTest.java
@WebMvcTest
class CTest {
...
}
我尝试过申请@DirtiesContext
根据此处的建议,了解受影响的测试类及其方法带有少量集成测试的 @SpyBean 无法正常工作 https://stackoverflow.com/questions/59776044/spybean-with-few-integration-tests-doesnt-work-correctly但没有结果。
As the @SpyBean
不为 null 并且不拦截方法调用,它必须以某种方式错误地初始化。
我已经解决了 Maven 配置问题,如此处所述https://stackoverflow.com/a/68619944/971355 https://stackoverflow.com/a/68619944/971355每个测试都在单独的虚拟机上进行,但我不喜欢该解决方案,因为它肯定会减慢测试执行速度。
None
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)