9

When I create Spring Boot Application it generates 2 classes:

@SpringBootApplication
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

And the second on is test:

@RunWith(SpringRunner.class)
@SpringBootTest
public class AppTest {

    @Test
    public void contextLoads() {
    }

}
  1. Like you can notice contextLoads test is empty. How should I provide correct test for contextLoad? It should stay empty maybe? Is it correct? Or I should put something inside?

UPDATE:

  1. Why this test should stay? If I have more tests in application I'll know if spring context is loaded or nope. Isn't it just excessive.

I readed answers like this but it didn't gave me a clear answer.

Community
  • 1
  • 1
degath
  • 1,530
  • 4
  • 31
  • 60

3 Answers3

9

Actually, the main() method of the Spring Boot Application is not covered by the unit/integration tests as the Spring Boot tests don't invoke the main() method to start the Spring Boot application.

Now I consider that all answers of this post seem overkill.
They want to add a test of the main() method to make a metric tool happy (Sonar).
Loading a Spring context and loading the application takes time.
Don't add it in each developer build just to win about 0.1% of coverage in your application.
I added an answer about that.


Beyond your simple example and the other post that you refer to, in absolute terms it may make sense to create a test for the main() method if you included some logic in. It is the case for example as you pass specific arguments to the application and that you handle them in the main() meethod.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
7

Leave it empty. If an exception occurs while loading the application context the test will fail. This test is just saying "Will my application load without throwing an exception"

Update for Second Question

The reason you want to keep this test is that when something fails you want to find the root cause. If you application context is unable to load, you can assume all other tests that are failing are due to this. If the above test passes, you can assume that the failing tests are unrelated to loading the context.

jax
  • 37,735
  • 57
  • 182
  • 278
-1

When you build a Spring boot application using Spring Initializer. It auto creates Application and its Test Class

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

@SpringBootTest
class ApplicationTest {

  @Test
  void contextLoads() {
  }
}

Note the use of @SpringBootTest annotation on test class 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.

You do not need to provide any test cases for empty method as such. Though 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();
    }
}
Ashish Lahoti
  • 648
  • 6
  • 8