java - 如何对我的所有集成测试文件执行 BeforeEach
问题描述
我想在所有集成测试文件的每个 @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 很陌生,我将不胜感激。谢谢。
解决方案
您可以创建超类,例如 BaseTest 并将此代码移到那里。然后你的每一个测试都应该扩展 BaseTest。更多的你可以在这个类中设置所有的 Annotation。例如:
@AutoConfigureMockMvc
@MockitoSettings(strictness = Strictness.STRICT_STUBS)
@ExtendWith(MockitoExtension.class)
@ExtendWith(SpringExtension.class)
@ActiveProfiles("test")
@SpringBootTest
public class BaseTest {
@MockBean
RequestInterceptor interceptor; // I NEED THIS
@BeforeEach
public void initTest() throws Exception {
Mockito.when(interceptor.preHandle(any(), any(), any())).thenReturn(true); // AND THIS
}
}
然后你所有的测试:
class MeasurementsServiceTest extends BaseTest {
//test methods here
}
推荐阅读
- customization - 如何在 erpnext 中进行自定义集成
- python - 如何从数据框中的另一列按条件创建新的组?
- sql - sql中的最大数字字符串数据类型
- amazon-web-services - Flutter - 如何从 AWS 获取和显示 pdf?
- php - 如何在 laravel 中验证 SignedRoute
- javascript - 在经典 ASP 页面中调用 .js 函数
- android - 错误:“您的 Android App Bundle 使用错误的密钥签名。” 如何使用 Google 提供的签名信息签署 apk/aab
- mysql - 如何为 JSON_OBJECT() 动态提供键和值?
- matlab - 如何估计回归系数定义我自己的协方差矩阵的估计方法或使用 MATLAB 给定的协方差矩阵?
- python - 使用 Python 将数据帧的行与字典配对