java - 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);
}
}
解决方案
推荐阅读
- angular - 在角度通用中未发现管道错误
- swift - 快速将 UInt8 数组的一段包装为字符串的正确方法是什么?
- excel-formula - 在日期范围内查找excel
- node.js - Nodejs 后端中的电子商务产品过滤器
- karma-runner - 5.2.3 版本的 Karma 漏洞问题
- rx-java - 在同一个调度程序上订阅时,有没有办法保留主题发出顺序?
- javascript - 如何在 mongodb 的单独网页上创建单独的集合?
- c# - C#:获取静态成员的名称
- pytest - pytest html 报告文件中的更改不会影响诱惑报告
- python - TypeError:在 Pyspark 的 DF 列上运行选择时,“NoneType”对象不可迭代