java - Spring Boot 1.4 - TestRestTemplate 不满足依赖异常

标签 java spring exception spring-boot autowired

有一个非常轻的 Spring Boot 1.4 项目,生成自 start.spring.io .

尝试使用 TestRestTemplate@RestController@RequestBody 运行集成测试,但由于启动异常没有成功。

唯一的配置类:

@SpringBootApplication
public class Application {

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

配置文件 application.properties 除了用于测试目的的 security.ignored=/** 外几乎什么都没有。

测试类:

@RunWith(SpringRunner.class)
@SpringBootTest
@DataJpaTest
public class MyControllerTest {

    private Logger log = Logger.getLogger(getClass());

    @Autowired
    private TestRestTemplate restTemplate;

    @Autowired
    private TestEntityManager entityManager;

    @Before
    public void init() {

        log.info("Initializing...");
    }

    @Test
    public void addTest() throws Exception {

        log.info("MyController add test starting...");

        // restTemplate usage

        log.info("MyController add test passed");
    }
}

...但是在测试启动期间我得到以下异常:

ERROR 6504 --- [           main] o.s.test.context.TestContextManager      : Caught exception while allowing TestExecutionListener [org.springframework.boot.test.autoconfigure.AutoConfigureReportTestExecutionListener@5444f1c3] to prepare test instance [com.myproject.controllers.MyControllerTest@5d2bc446]

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.myproject.controllers.MyControllerTest': Unsatisfied dependency expressed through field 'restTemplate': No qualifying bean of type [org.springframework.boot.test.web.client.TestRestTemplate] found for dependency [org.springframework.boot.test.web.client.TestRestTemplate]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.boot.test.web.client.TestRestTemplate] found for dependency [org.springframework.boot.test.web.client.TestRestTemplate]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:569) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]

根据doc不需要在任何地方配置 TestRestTemplate。但是,我已经按照它的建议将最新的 Apache Http Client 添加到类路径中。

我错过了什么?

最佳答案

您正在指定 @DataJpaTest,它告诉 Spring 排除测试的 web 上下文的任何连接。因此,没有创建 TestRestTemplate。阅读此博客以了解有关测试应用程序切片的更多详细信息:https://spring.io/blog/2016/04/15/testing-improvements-in-spring-boot-1-4#testing-application-slices

关于java - Spring Boot 1.4 - TestRestTemplate 不满足依赖异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38945940/

相关文章:

java - 如何从 try、catch 和 finally 中返回一个值?

java - 文档 - 如何通过名称获取标签的值?

java - Camel-Spring 测试时间路线排除

java - 如何使用Spring操作生产数据库数据格式?

java - Spring在资源中找不到文件

android - 使用异步任务

Java jjwt 为 JWT 创建无效 JSON

java - 如何让 Hibernate 忽略一个方法?

java - 出现错误 "org.apache.jasper.el.JspMethodNotFoundException"

c# - 异常未在 Async/Await block 中捕获