[Spring Boot Test] Spring Boot Test

Spring Boot Test

FAQs

No qualifying bean of type ‘org.springframework.boot.test.web.client.TestRestTemplate’ available

1
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.boot.test.web.client.TestRestTemplate' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

TestRestTemplate is only auto-configured when @SpringBootTest has been configured with a webEnvironment that means it starts the web container and listens for HTTP requests. For example:

Note the use of webEnvironment=RANDOM_PORT to start the server with a random port (useful to avoid conflicts in test environments)

1
2
3
4
5
6
7
8
9
10
11
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class DemoServiceTest {

@Autowired
private DemoService demoService;

@Test
public void testCreate() {
demoService.Create();
}
}

References

[1] Getting Started | Testing the Web Layer - https://spring.io/guides/gs/testing-web/