gpt4 book ai didi

java - SpringBoot2 : the problem with AspectJ during the test running

转载 作者:行者123 更新时间:2023-12-05 06:23:18 29 4
gpt4 key购买 nike

我有一个带有小型 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

最佳答案

试试这个:

@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
}
}

关于java - SpringBoot2 : the problem with AspectJ during the test running,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58544344/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com