JUnit& IntelliJ:无法自动装配。找不到类型的豆子

时间:2018-05-12 19:32:30

标签: java spring-boot intellij-idea junit

我试图对此GitHub存储库中的代码运行基本单元测试found here

由于以下错误,MultiplicationServiceTest中的测试失败:

Caused by:     org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'microservices.book.multiplication.service.MultiplicationService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1493)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
... 28 more

我使用的IDE是IntelliJ,在一些Google搜索中,我发现在测试用例中使用@Autowire时,它经常报告错误。该错误正在影响MultiplicationServiceTest类中的multiplicationService变量,我不确定原因。我尝试使用@Repository标记MultiplicationService接口,但这并没有解决错误。任何帮助表示赞赏。

MultiplicationServiceTest:

package microservices.book.multiplication.service;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;

import microservices.book.multiplication.domain.Multiplication;
import microservices.book.multiplication.service.MultiplicationService;
import microservices.book.multiplication.service.RandomGeneratorService;

@RunWith(SpringRunner.class)
@SpringBootTest
public class MultiplicationServiceTest {

    @MockBean
    private RandomGeneratorService randomGeneratorService;

    @Autowired
    private MultiplicationService multiplicationService;

    @Test
    public void contextLoads() {

    }

}

MultiplicationService:

package microservices.book.multiplication.service;

import microservices.book.multiplication.domain.Multiplication;

public interface MultiplicationService {

    Multiplication createRandomMultiplication();

}

乘:

package microservices.book.multiplication.domain;

public class Multiplication {

    private int factorA;
    private int factorB;
    private int result;

    public Multiplication(int factorA, int factorB, int result) {
        this.factorA = factorA;
        this.factorB = factorB;
        this.result = result;
    }

}

SocialMultiplicationApplication:

package microservices.book.multiplication;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SocialMultiplicationApplication {

    public static void main(String[] args) {
        SpringApplication.run(SocialMultiplicationApplication.class, args);
    }
}

0 个答案:

没有答案