java - Spring Boot mvnw 无法编译代码成功 test-compiled
问题描述
我正在使用 TestRestTemplate 运行 Spring Boot 测试。我的代码具有以下自动装配:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment=WebEnvironment.DEFINED_PORT)
class TestMyService
{
...
@Autowired
private TestRestTemplate temp;
private int port = 8080;
...
我的测试使用 temp 对象进行了基本的 POST:
String json = temp..postForObject("http://localhost:"+port,params,String.class);
我的测试编译并运行没有问题,但是当我尝试使用以下方法编译 Jar 文件时:
mvnw clean package
编译失败并出现以下故障:
[ERROR] basicLetterService(com.factor3.app.counter.TestMyService) Time elapsed: 0.001 s <<< ERROR!
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.factor3.app.counter.TestMyService': Unsatisfied dependency expressed through field 'temp'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.boot.test.web.client.TestRestTemplate' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.boot.test.web.client.TestRestTemplate' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
请注意,失败是由于 Boot 无法自动装配已声明并成功编译以运行测试的 TestRestTemplate 对象。
为什么会出现这种情况?我需要做一些配置细节吗?
解决方案
推荐阅读
- database - Flutter Firebase 数据库“在 null 上调用了方法‘forEach’。”
- java - Netbeans 11.3 IDE 中的 Junit 测试失败“找不到或加载主类”
- delphi - 我可以在不安装软件包的情况下按原样使用 Indy10
- python - Python(django)正则表达式不起作用
- python-3.6 - pykafka 在nosetests 集成测试上的死锁
- mongodb - 适用于开发人员的 AWS IAM 用户访问权限
- material-ui - 具有矩形背景的材料 IU 图标
- java - 抛出异常java时无法继续程序执行
- load-balancing - 如何在我的 Rasa 聊天机器人中进行负载平衡?
- php - Laravel 停止配置自动缓存