【发布时间】:2020-12-31 22:08:25
【问题描述】:
我想在所有集成测试文件的每个 @BeforeEach 中运行一些代码。基本上我需要添加的代码如下:
@MockBean
RequestInterceptor interceptor; // I NEED THIS
@BeforeEach
public void initTest() throws Exception {
Mockito.when(interceptor.preHandle(any(), any(), any())).thenReturn(true); // AND THIS
}
有没有办法避免在每个文件中重复这部分?也许我可以创建一个测试配置文件并在我的测试文件中使用注释。由于我对 java spring boot 很陌生,我将不胜感激。谢谢。
【问题讨论】:
-
好吧,如果我正确理解您的问题,您可以创建一个具有 BeforeEach 方法的父基类,然后在所有其他基类上继承该类...
-
您还可以使用 JUnit5 的扩展或 Junit 的 Runners JUnit 5 guide
标签: java spring spring-boot junit integration-testing