When you build a Spring boot application using Spring Initializer. It auto creates a test class for you with contextLoads
empty method.
@SpringBootTest
class ApplicationContextTest {
@Test
void contextLoads() {
}
}
Note the use of @SpringBootTest
annotation which tells Spring Boot to look for a main configuration class (one with @SpringBootApplication, for instance) and use that to start a Spring application context. Empty contextLoads()
is a test to verify if the application is able to load Spring context successfully or not.
If sonarqube is complaining about the empty method then you can do something like this to verify your controller or service bean context:-
@SpringBootTest
public class ApplicationContextTest {
@Autowired
private MyController myController;
@Autowired
private MyService myService;
@Test
public void contextLoads() throws Exception {
assertThat(myController).isNotNull();
assertThat(myService).isNotNull();
}
}