2016-08-14 23 views
6

Có một dự án Spring Boot 1.4 rất nhẹ, được tạo từ start.spring.io.Spring Boot 1.4 - TestRestTemplate Ngoại lệ phụ thuộc không hài lòng

Cố gắng chạy thử nghiệm tích hợp cho @RestController với @RequestBody sử dụng TestRestTemplate, nhưng không thành công do ngoại lệ khởi động.

duy nhất lớp cấu hình:

@SpringBootApplication 
public class Application { 

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

Cấu hình tập tin application.properties hầu như không có gì ngoại trừ của security.ignored=/** cho mục đích thử nghiệm.

Lớp kiểm tra:

@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"); 
    } 
} 

... nhưng trong quá trình khởi động thử nghiệm tôi nhận được ngoại lệ sau đây:

ERROR 6504 --- [   main] o.s.test.context.TestContextManager  : Caught exception while allowing TestExecutionListener [org.springframework.b[email protected]5444f1c3] to prepare test instance [[email protected]] 

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] 

Theo doc nó không cần thiết để cấu hình TestRestTemplate bất cứ nơi nào. Tuy nhiên, tôi đã thêm Apache Http Client mới nhất vào classpath như nó được đề xuất.

Tôi đã bỏ lỡ điều gì?

+0

Chỉ cần đã tìm thấy '@ AutoConfigureTestDatabase' chú thích nên được sử dụng trong conjuction với' @ SpringBootTest' thay vì '@ DataJpaTest' trong trường hợp như thế này – WildDev

Trả lời

0

Tôi đã có một vấn đề tương tự chạy các lớp học chính trên Eclipse sử dụng Serenity BDD thử nghiệm với một mùa xuân-boot. Nó bắt đầu thất bại sau khi tôi đã thêm phụ thuộc kiểm tra spring-boot-test-autoconfigure. Điều đó xảy ra vì Eclipse đặt mọi thứ chỉ trong một trình nạp lớp. Để khắc phục lỗi này, tôi đã tạo một lớp cấu hình ghi đè hành vi mặc định của spring-boot. Mã này đã được đặt tại một lớp học mùa xuân (phạm vi không phải là công) 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; 
    } 
} 
Các vấn đề liên quan