I had the same goal (to have a test that runs the main () method), and I noticed that simply adding a test method like @ fg78nc actually βlaunchesβ the application twice: once using the spring boot test environment , once through an explicit call to mainApp.main(new String[] {}) , which I do not find elegant.
In the end, I wrote two test classes: one with the annotation @SpringBootTest and an empty test method applicationContextLoaded (), the other without @SpringBootTest (only RunWith(SpringRunner.class) ), which calls the main method.
SpringBootApplicationTest
package example; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.boot.test.context.SpringBootTest; @RunWith(SpringRunner.class) @SpringBootTest public class SpringBootApplicationTest { @Test public void contextLoads() { } }
ApplicationStartTest
package example; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) public class ApplicationStartTest { @Test public void applicationStarts() { ExampleApplication.main(new String[] {}); } }
In general, the application is still running twice, but because now there are two test classes. Of course, with just these two testing methods, this seems superfluous, but usually more tests will be added to the SpringBootApplicationTest class, taking advantage of the @SpringBootTest installation.
marcpa00
source share