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() {
}
}
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' –
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. –
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 –