Spring Boot 1.4 - TestRestTemplate无法满足依赖异常

6

我有一个非常轻量级的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]

根据文档,不需要在任何地方配置TestRestTemplate。但是,我已经按建议将最新的Apache Http客户端添加到类路径中。我错过了什么?

刚刚发现在这种情况下应该将@AutoConfigureTestDatabase注解与@SpringBootTest一起使用,而不是与@DataJpaTest一起使用。 - WildDev
2个回答

5

0

我在使用 Serenity BDD 测试和 spring-boot 时,在 Eclipse 上运行主类时遇到了类似的问题。在添加了 spring-boot-test-autoconfigure 测试依赖项后,它开始失败。这是因为 Eclipse 将所有内容都放在一个类加载器中。为了解决这个错误,我创建了一个配置类,覆盖了 spring-boot 的默认行为。这段代码基于一个 spring 类(作用域不是公共的)SpringBootTestContextCustomizer.TestRestTemplateFactory

@TestConfiguration
public class TestConfig {

    // Overriding Default Spring Boot TestRestTemplate to allow 
    // execute the main method from Eclipse (mixed Classloader) 
    @Bean
    @Primary
    public TestRestTemplate testRestTemplate(ApplicationContext context, RestTemplateBuilder templateBuilder) {
        final AbstractConfigurableEmbeddedServletContainer container = context.getBean(AbstractConfigurableEmbeddedServletContainer.class);
        final boolean sslEnabled = container.getSsl() != null && container.getSsl().isEnabled();
        final TestRestTemplate template = new TestRestTemplate(templateBuilder.build(), null, null, sslEnabled? new HttpClientOption[]{}: new HttpClientOption[]{HttpClientOption.SSL});
        template.setUriTemplateHandler(new LocalHostUriTemplateHandler(context.getEnvironment(), sslEnabled ? "https" : "http"));
        return template;
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接