Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I test the main() method of Spring Boot Application and how?

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.

like image 462
degath Avatar asked Mar 18 '18 07:03

degath


People also ask

Should I test main method?

A main method is still code which can contain bugs and causing undesired application behavior. So, testing a main method can make sense in the same way testing every other method does. Unfortunately, a main method is a static method which makes unit testing without additional frameworks not possible.

Do we need main method in spring boot?

A Spring Boot application's main class is a class that contains a public static void main() method that starts up the Spring ApplicationContext. By default, if the main class isn't explicitly specified, Spring will search for one in the classpath at compile time and fail to start if none or multiple of them are found.

How do you unit test a spring boot main class?

First workaround : make your Spring Boot application class with no bean declared inside. If you have them, move them in a configuration class (for make them still cover by unit test). And then ignore your Spring Boot application class in the test coverage configuration. yeah you are right.


2 Answers

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.

like image 143
davidxxx Avatar answered Sep 20 '22 22:09

davidxxx


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.

like image 35
jax Avatar answered Sep 21 '22 22:09

jax