问题描述
我使用 Spring Boot 2.1.6.RELEASE 在 Spring 中创建了一个简单的方面.它基本上记录了在一个方法上花费的总时间.
I created a simple aspect in Spring using Spring Boot 2.1.6.RELEASE.It basically logs the total time spent on a method.
@Aspect
@Component
public class TimeLoggerAspect {
private static final Logger log = LoggerFactory.getLogger(TimeLoggerAspect.class);
@Around("@annotation(demo.TimeLogger)")
public Object methodTimeLogger(ProceedingJoinPoint joinPoint)
throws Throwable {
long startTime = System.currentTimeMillis();
Object proceed = joinPoint.proceed();
long totalTime = System.currentTimeMillis() - startTime;
log.info("Method " + joinPoint.getSignature() + ": " + totalTime + "ms");
return proceed;
}
}
方面由 TimeLogger
注释触发
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface TimeLogger {
}
并在这样的组件中使用
@Component
public class DemoComponent {
@TimeLogger
public void sayHello() {
System.out.println("hello");
}
}
Spring Boot 演示应用程序将通过 CommandLineRunner
接口的 run
方法调用 sayHello
.
A spring boot demo application will invoke sayHello
via the run
method of the CommandLineRunner
interface.
@SpringBootApplication
public class DemoApplication implements CommandLineRunner {
@Autowired
private DemoComponent demoComponent;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
demoComponent.sayHello();
}
}
为了完整起见,我在 build.gradle
中添加了我的修改:为 aop、spring test 和 jupiter (junit) 添加库.
For completeness, I add my modifications in build.gradle
: adding libraries for aop, spring test and jupiter (junit).
compile("org.springframework.boot:spring-boot-starter-aop")
testCompile("org.springframework.boot:spring-boot-starter-test")
testCompile("org.junit.jupiter:junit-jupiter-api")
testRuntime("org.junit.jupiter:junit-jupiter-engine")
运行应用程序将输出(为了可读性进行了修剪)
Running the application will output (trimmed for readability)
hello
... TimeLoggerAspect : Method void demo.DemoComponent.sayHello(): 4ms
到目前为止,一切都很好.现在我基于 @SpringBootTest
注释和 jupiter 创建一个测试.
So far, so good. Now I create a test based on @SpringBootTest
annotation and jupiter.
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = {DemoComponent.class, TimeLoggerAspect.class})
public class DemoComponentFailTest {
@Autowired
private DemoComponent demoComponent;
@Test
public void shouldLogMethodTiming() {
demoComponent.sayHello();
}
}
在这里我得到输出
hello
TimeLoggerAspect
没有输出,因为它似乎没有被触发.
No output from the TimeLoggerAspect
, since it seems it is not being triggered.
在测试中是否缺少触发方面的东西?或者还有其他方法可以在spring boot中测试aspect吗?
Is something missing to trigger the aspect in the test? Or are there other ways of testing the aspect in spring boot?
推荐答案
我遇到了类似的问题.我的方面正在监听控制器方法.要激活它,导入 AnnotationAwareAspectJAutoProxyCreator
就可以了:
I had similar problem. My Aspect is listening on controller methods. To get it activated, importing the AnnotationAwareAspectJAutoProxyCreator
made the trick:
@RunWith(SpringRunner.class)
@Import(AnnotationAwareAspectJAutoProxyCreator.class) // activate aspect
@WebMvcTest(MyController.class)
public class MyControllerTest {
...
}
这篇关于如何使用 SpringBootTest 测试方面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!