I am using spring boot 1.4.0.RELEASE
. I am writing tests for my controller class. I get the following exception.
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)}
Here is my test class
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() { } }
MockMVC class is part of Spring MVC test framework which helps in testing the controllers explicitly starting a Servlet container. In this MockMVC tutorial, we will use it along with Spring boot's WebMvcTest class to execute Junit testcases which tests REST controller methods written for Spring boot 2 hateoas example.
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:
@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)
If you read the java doc of SpringBootTest annotation, it says that annotation provides below features (not listing all of them here, but only what are relevant to the question.)
So @SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT) provides the ability to autowire TestRestTemplate because it starts a fully running web server [as mentioned in @AndyWilkinson' answer as well].
But if you want to autowire MockMvc as well in same TestClass then use @AutoConfigureMockMvc annotation over TestClass.
This is how a Test class may look like:
@RunWith(SpringRunner.class) @SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT) @AutoConfigureMockMvc public class SBTest { @Autowired private TestRestTemplate restTemplate; @Autowired private MockMvc mvc; // tests }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With