【发布时间】:2021-02-21 05:27:17
【问题描述】:
我有一个带有小型 AspectJ 类的项目:
package com.blabla.joy.aspect;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class SomeAspect {
public SomeAspect() {
}
}
还有一个小测试类:
package com.blabla.joy.aspect;
import com.blabla.joy.MainSpringBoot;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = {MainSpringBoot.class})
public class SomeTest {
@MockBean
private SomeAspect someAspect;
@Test
public void someTest(){
//test something
}
}
主要的应用类是:
package com.blabla.joy;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@SpringBootApplication(exclude = MultipartAutoConfiguration.class)
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class MainSpringBoot implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(MainSpringBoot.class, args);
}
@Override
public void run(String... arg0) throws Exception {
System.out.println("i'm running!!!");
}
}
当我运行 SomeTest 时出现以下错误:
java.lang.IllegalStateException: Failed to load ApplicationContext
...
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mainSpringBoot': BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.aop.framework.AopConfigException: [com.blabla.joy.aspect.SomeAspect$MockitoMock$2005048991] cannot extend concrete aspect [com.blabla.joy.aspect.SomeAspect]
...
Caused by: org.springframework.aop.framework.AopConfigException: [com.blabla.joy.aspect.SomeAspect$MockitoMock$2005048991] cannot extend concrete aspect [com.blabla.joy.aspect.SomeAspect]
...
这是什么意思?我应该在我的方面、测试或主类中添加一些特殊的逻辑(注释等)吗?
P.S:我在项目中使用 SpringBoot2
【问题讨论】:
标签: java spring-boot mocking aop aspectj