如何用SpringBootTest测试一个方面

如何用SpringBootTest测试一个方面

本文介绍了如何用SpringBootTest测试一个方面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用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测试和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批注和木星创建一个测试.

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中测试方面?

Is something missing to trigger the aspect in the test? Or are there other ways of testing the aspect in spring boot?

推荐答案

您必须将@EnableAspectJAutoProxy与文件@Configuration一起使用,该文件使用@Aspect声明Bean.

You have to put @EnableAspectJAutoProxy with your file @Configuration that declares the bean with @Aspect.

@Aspect
@Configuration
@EnableAspectJAutoProxy
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;
  }
}

我认为那样就可以了.

这篇关于如何用SpringBootTest测试一个方面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 19:19