Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test main class of Spring-boot application

I have a spring-boot application where my @SpringBootApplication starter class looks like a standard one. So I created many tests for all my functionalities and send the summary to sonarqube to see my coverage.

For my starter class Sonarqube tells me that I just have 60% coverage. So the average coverage is not good as expected.

enter image description here

My Test class is just the default one.

@RunWith(SpringRunner.class) @SpringBootTest(classes = ElectronicGiftcardServiceApplication.class) public class ElectronicGiftcardServiceApplicationTests {      @Test     public void contextLoads() {     } } 

So how can I test my main class in the starter class of my application?

like image 372
Patrick Avatar asked Oct 09 '17 15:10

Patrick


People also ask

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.

How do you write test cases for spring boot application class?

Then, configure the Application context for the tests. The @Profile(“test”) annotation is used to configure the class when the Test cases are running. Now, you can write a Unit Test case for Order Service under the src/test/resources package. The complete code for build configuration file is given below.

What is test class in spring boot?

class) provides a bridge between Spring Boot test features and JUnit. Whenever we are using any Spring Boot testing features in our JUnit tests, this annotation will be required.


Video Answer


2 Answers

All these answers seem overkill.
You don't add tests to make a metric tool happy.
Loading a Spring context of the application takes time. Don't add it in each developer build just to win about 0.1% of coverage in your application.
Here you don't cover only 1 statement from 1 public method. It represents nothing in terms of coverage in an application where thousands of statements are generally written.

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.

Second workaround : if you really need to to cover the main() invocation (for organizational reasons for example), create a test for it but an integration test (executed by an continuous integration tool and not in each developer build) and document clearly the test class purpose :

import org.junit.Test;  // Test class added ONLY to cover main() invocation not covered by application tests. public class MyApplicationIT {    @Test    public void main() {       MyApplication.main(new String[] {});    } } 
like image 197
davidxxx Avatar answered Sep 21 '22 21:09

davidxxx


You can do something like this

@Test public void applicationContextLoaded() { }  @Test public void applicationContextTest() {     mainApp.main(new String[] {}); } 
like image 28
fg78nc Avatar answered Sep 20 '22 21:09

fg78nc