2016-08-29 21 views
21

Tôi đang sử dụng khởi động mùa xuân 1.4.0.RELEASE. Tôi đang viết bài kiểm tra cho lớp điều khiển của tôi. Tôi nhận được ngoại lệ sau đây.thử nghiệm khởi động mùa xuân không thể tiêm TestRestTemplate và MockMvc

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.concur.cognos.authentication.service.ServiceControllerITTest': 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)} 

Đây là lớp học thử nghiệm của tôi

public class ServiceControllerITTest extends ApplicationTests { 

    @Autowired 
    private TestRestTemplate restTemplate; 

    @Autowired 
    private MockMvc mvc; 

    @Test 
    public void exampleTest() throws Exception { 
     // test 
    } 
} 

ApplicationTests.java

@RunWith(SpringRunner.class) 
@SpringBootTest 
@WebAppConfiguration 
//@DirtiesContext 
public class ApplicationTests { 

    @Autowired 
    Environment env; 

    @Test 
    public void contextLoads() { 

    } 

} 

Trả lời

40

TestRestTemplate chỉ tự động cấu hình khi @SpringBootTest đã được cấu hình với một webEnvironment đó có nghĩa là nó bắt đầu những trang web chứa và lắng nghe cho các yêu cầu HTTP. Ví dụ:

@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT) 
+0

Tôi đã thêm. bây giờ tôi nhận được 'java.lang.IllegalStateException: Không tải được ApplicationContext \t tại org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext' –

+1

Bạn vẫn đang cố gắng tự động' TestRestTemplate' và 'MockMvc'? Làm như vậy không có ý nghĩa. Bạn nên sử dụng cái này hay cái kia. –

+0

Nó cũng không giúp được gì. Tôi đã nhận xét 'MockMVC' và chỉ tự động trả lời' TestRestTemplate'. Tôi vẫn nhận được lỗi tương tự như trong chú thích ở trên –

3

Để làm việc với điều đó, không sử dụng biểu mẫu TestRestTemplate không được dùng nữa.

Deprecated:

import org.springframework.boot.test.TestRestTemplate; 

Đúng:

import org.springframework.boot.test.web.client.TestRestTemplate; 

Sau đó, bạn có thể sử dụng @Autowired chú thích trong lớp học của bạn:

@Autowired 
private TestRestTemplate restTemplate; 

Và đừng sử dụng:

@Autowired 
private MockMvc mvc; 

Cả hai cùng nhau không hoạt động.

0

Theo Spring boot documentation:

Bạn cũng có thể tự động cấu hình MockMvc trong một phi @WebMvcTest (ví dụ SpringBootTest) bằng cách chú thích nó với @AutoConfigureMockMvc.

Các vấn đề liên quan