SpringBoot2: проблема с AspectJ во время выполнения теста - PullRequest
0 голосов
/ 24 октября 2019

У меня есть проект с небольшим классом 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]
...

Что это значит? Должен ли я добавить некоторую специальную логику (аннотации и т. Д.) В мой аспект, тест или основной класс?

PS: я использую SpringBoot2 в проекте

1 Ответ

0 голосов
/ 24 октября 2019

Попробуйте это:

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