I'm using Spring Boot to create a REST API and write some unit tests on my controllers. I know that the recommended manner to inject beans in spring is the constructor injection. But when i add the @SpringBootTest annotation to my test class, I can not inject my controller class with constructor, I find myself obliged to use @Autowired.
Have some explanation and is there another way to use constructor injection with SpringBootTest.
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) class PersonControllerTest { @LocalServerPort private int port; @Autowired private PersonController controller; @Autowired private TestRestTemplate restTemplate; @Test public void greetingShouldReturnDefaultMessage() throws Exception { assertThat(this.restTemplate.getForObject("http://localhost:" + port + "/cvtech/Persons/", String.class)).contains("content"); } @Test public void contextLoads() throws Exception { assertThat(controller).isNotNull(); } @Test void findAllByJob() { } }