在这里是我的注释:
@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface TestAnnotation {
}
这里是我的方面:
@Aspect
@Component
public class TestAspect {
public TestAspect() {
// this is called - so @Component works
System.out.println("aspect constructor called");
}
@Around("@annotation(TestAnnotation)")
public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
// this is NOT called in the test - @Around did not work
System.out.println("aroundAdvice() called");
return joinPoint.proceed();
}
}
这是我的测试:
@SpringBootTest
@ComponentScan(basePackages = { "de.mycompany" }) // root of all my packages
@TestPropertySource(value = "classpath:application-test.yml", factory = YamlPropertySourceFactory.class)
@AutoConfigureMockMvc
class MyTestClass {
@Autowired
private MockMvc mockMvc;
@Test
@TestAnnotation
void testSomething() throws Exception {
System.out.println("testSomething() called.");
// test something with mockMvc
}
}
如果我注释RestController中的端点,则每当调用或测试该端点时,都会触发。 但是当调用我的测试方法(此处:
aroundAdvice()
)时,我需要触发它。这是行不通的。
在testSomething()
中使用完全合格的班级也无效:
@Around
致命仅在豆类上起作用。他们通过创建代理来起作用。您的
@Around("@annotation(de.mycompany.annotation.TestAnnotation)")
不是真正的春豆。实施您想要的一种方法是实现扩展名,并在
MyTestClass
中检查注释。
InvocationInterceptor