【发布时间】:2019-08-08 13:31:28
【问题描述】:
在我的 spring boot 项目中,我使用MockMVC 来测试控制器(web)层。但是我的项目中也有 AOP(AspectJ) 逻辑,当我使用 MockMVC 为控制器运行单元测试时,测试也会触发 AOP 代码,如何防止在为控制器运行单元测试时触发 AOP 代码?
@Test
public void testMyControllerMethod() {
...
// myRequest hits an endpoint function of my controller, there is also AOP intercept the function call, how can I disable AOP to be triggered while running test?
mockMVC.perform(myRequest).andExpect(okStatus)
}
问题在我的代码注释中:)
我查过this answer,我知道使用if() 表达式,但是我没有得到TestMode.ACTIVE,Spring boot 中没有这样的东西。如果有人可以让我知道如何检查代码是否在运行时运行单元测试,我也会知道如何防止 AOP 逻辑运行。
【问题讨论】:
-
是的,但答案对我不起作用,例如我怎么知道代码在测试模式下运行?那个回答提到
TestMode.ACTIVE,不知道从哪里来的,spring boot中没有TestMode类。 -
这是您在测试中设置的全局变量。方面必须检查这个变量。这是一个手动解决方案,而不是来自 Spring
-
您能否提供一个简单的 glable 变量的骨架示例?放在哪里以及在哪里更新值?
-
public final boolean TestMode.ACTIVE = false;并在测试中设置 TestMode.ACTIVE = true;
标签: spring-boot aspectj spring-aop