首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SpringBoot2: AspectJ在测试运行期间的问题

SpringBoot2: AspectJ在测试运行期间的问题
EN

Stack Overflow用户
提问于 2019-10-24 23:15:28
回答 1查看 1K关注 0票数 2

我有一个包含一个小AspectJ类的项目:

代码语言:javascript
复制
package com.blabla.joy.aspect;

import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class SomeAspect {
    public SomeAspect() {

    }
}

还有一个小的测试类:

代码语言:javascript
复制
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
    }
}

主要的应用程序类是:

代码语言:javascript
复制
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时,出现以下错误:

代码语言:javascript
复制
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]
...

什么意思?我应该添加一些特殊的逻辑(注解等)吗?对我来说,测试还是主类?

附言:我在项目中使用SpringBoot2

EN

回答 1

Stack Overflow用户

发布于 2019-10-25 00:43:12

试试这个:

代码语言:javascript
复制
@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = {MainSpringBoot.class})
public class SomeTest {

    @Autowired
    @InjectMocks
    private SomeAspect someAspect;

    @Before
    public void init(){
        MockitoAnnotations.initMocks(this)
    }

    @Test
    public void someTest(){
        //test something
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58544344

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档