首页 > 解决方案 > JUnit 和 IntelliJ:无法自动装配。找不到类型的 bean

问题描述

我正在尝试对此处找到的 GitHub 存储库中的代码运行基本单元测试

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,从一些谷歌搜索中我看到在测试用例中使用 @Autowire 时它经常报告错误。该错误正在影响 MultiplicationServiceTest 类中的 multiplicationService 变量,我不确定原因。我尝试使用 @Repository 标记 MultiplicationService 接口,但这并不能解决错误。任何帮助表示赞赏。

乘法服务测试:

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() {

    }

}

乘法服务:

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

}

社会乘法应用:

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);
    }
}

标签: javaspring-bootintellij-ideajunit

解决方案


推荐阅读