title: How to test an aspect with SpringBootTest?
body: <p>I created a simple aspect in Spring using Spring Boot 2.1.6.RELEASE.
  It basically logs the total time spent on a method.</p>
  
  <pre><code>@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;
    }
  }
  </code></pre>
  
  <p>the aspect is triggered by a <code>TimeLogger</code> annotation</p>
  
  <pre><code>@Retention(RetentionPolicy.RUNTIME)
  @Target(ElementType.METHOD)
  public @interface TimeLogger {
  }
  </code></pre>
  
  <p>and is used in a component like this</p>
  
  <pre><code>@Component
  public class DemoComponent {
    @TimeLogger
    public void sayHello() {
      System.out.println("hello");
    }
  }
  </code></pre>
  
  <p>A spring boot demo application will invoke <code>sayHello</code> via the <code>run</code> method of the <code>CommandLineRunner</code> interface.</p>
  
  <pre><code>@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();
    }
  }
  </code></pre>
  
  <p>For completeness, I add my modifications in <code>build.gradle</code>: adding libraries for aop, spring test and jupiter (junit).</p>
  
  <pre><code>    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")
  </code></pre>
  
  <p>Running the application will output (trimmed for readability)</p>
  
  <pre><code>hello
  ... TimeLoggerAspect : Method void demo.DemoComponent.sayHello(): 4ms
  
  </code></pre>
  
  <p>So far, so good. Now I create a test based on <code>@SpringBootTest</code> annotation and jupiter.</p>
  
  <pre><code>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();
    }
  }
  </code></pre>
  
  <p>and here I get the output</p>
  
  <pre><code>hello
  </code></pre>
  
  <p>No output from the <code>TimeLoggerAspect</code>, since it seems it is not being triggered.</p>
  
  <p>Is something missing to trigger the aspect in the test? Or are there other ways of testing the aspect in spring boot?</p>