aop@Aspemp未在 @springboottest

问题描述 投票:0回答:1
如何在测试执行过程中不会触发我的@around方法。 如果我注释,例如REST控制器内部的端点方法比触发。但是我想将其用于测试。

在这里是我的注释:

@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
java spring-boot aop spring-aop spring-annotations
1个回答
0
投票
将该扩展名通过在测试类中添加到测试中。


最新问题
© www.soinside.com 2019 - 2025. All rights reserved.