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ì?
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