Spring Boot 1.4 - TestRestTemplate 不满足依赖异常
Spring Boot 1.4 - TestRestTemplate Unsatisfied dependency exception
有一个非常轻的 Spring Boot 1.4 项目,生成自 start.spring.io。
正在尝试 运行 使用 TestRestTemplate
与 @RequestBody
进行 @RestController
的集成测试,但由于启动异常没有成功。
唯一配置class:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
配置文件 application.properties
除了用于测试目的的 security.ignored=/**
外几乎什么都没有。
测试class:
@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 添加到推荐的 class 路径中。
我错过了什么?
您正在指定 @DataJpaTest
,这告诉 Spring 排除测试的 Web 上下文的任何连接。因此,没有创建 TestRestTemplate。阅读此博客以了解有关测试应用程序切片的更多详细信息:https://spring.io/blog/2016/04/15/testing-improvements-in-spring-boot-1-4#testing-application-slices
我遇到了类似的问题 运行 Eclipse 上的主要 class 使用 Serenity BDD 测试 spring -启动。添加 spring-boot-test-autoconfigure
测试依赖项后,它开始失败。发生这种情况是因为 Eclipse 将所有内容都放在一个 classloader 中。为了修复这个错误,我创建了一个配置 class 来覆盖 spring-boot 的默认行为。此代码基于一个 spring class(范围不是 public)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;
}
}
有一个非常轻的 Spring Boot 1.4 项目,生成自 start.spring.io。
正在尝试 运行 使用 TestRestTemplate
与 @RequestBody
进行 @RestController
的集成测试,但由于启动异常没有成功。
唯一配置class:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
配置文件 application.properties
除了用于测试目的的 security.ignored=/**
外几乎什么都没有。
测试class:
@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 添加到推荐的 class 路径中。
我错过了什么?
您正在指定 @DataJpaTest
,这告诉 Spring 排除测试的 Web 上下文的任何连接。因此,没有创建 TestRestTemplate。阅读此博客以了解有关测试应用程序切片的更多详细信息:https://spring.io/blog/2016/04/15/testing-improvements-in-spring-boot-1-4#testing-application-slices
我遇到了类似的问题 运行 Eclipse 上的主要 class 使用 Serenity BDD 测试 spring -启动。添加 spring-boot-test-autoconfigure
测试依赖项后,它开始失败。发生这种情况是因为 Eclipse 将所有内容都放在一个 classloader 中。为了修复这个错误,我创建了一个配置 class 来覆盖 spring-boot 的默认行为。此代码基于一个 spring class(范围不是 public)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;
}
}