Masz bardzo jasny projekt Spring Boot 1.4, wygenerowany z start.spring.io.Spring Boot 1.4 - TestRestTemplate Niezadowolony wyjątek zależności
Próba uruchomienia testu integracji dla @RestController
z @RequestBody
przy użyciu TestRestTemplate
, ale nie ma sukcesu z powodu wyjątku uruchamiania.
Jedyna klasa konfiguracja:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
plik konfiguracyjny application.properties
ma prawie nic z wyłączeniem security.ignored=/**
dla celów testowych.
Klasa Test:
@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");
}
}
... ale podczas uruchamiania testu pojawia się następujący wyjątek:
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]
Według doc to nie jest wymagane, aby skonfigurować TestRestTemplate
wszędzie. Jednak dodałem najnowszego klienta Apache Http do ścieżki klasowej zgodnie z zaleceniami.
Co przeoczyłem?
Właśnie znalazłeś '@ AutoConfigureTestDatabase' adnotacja powinna być stosowana w połączeniu z' @ SpringBootTest' zamiast '@ DataJpaTest' w przypadku lubię to – WildDev