How to JUnit Test Spring-Boot's Application.java

R.C :

Apologize as this might seems to be an useless act, but is there any way we can actually do junit test on Spring-Boot(1.3.8.RELEASE)'s Application.java which this class does nothing but to start a Spring-boot application?

Given below:

@SpringBootApplication
public class Application {

    public static void main(String[] args) {

       try{
             SpringApplication.run(Application.class, args);
       }catch(Exception e){
             //code here
       }   
    }
}

Maybe i can try catching exception? but what else can i possibly test so that JUnit will test out SpringApplication.run()? any example is appreciated. Thank you all!

user1024314 :

Usually, the following is sufficient to test that the application context starts up.

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class ApplicationTests {

    @Test
    public void contextLoads() {
    }
}

However, if you want to directly test Application.main, you can also test it this way:

public class ApplicationTest {

    @Test
    public void test()
    {
        Application.main(new String[]{
                "--spring.main.web-environment=false",
                "--spring.autoconfigure.exclude=blahblahblah",
                // Override any other environment properties according to your needs
        });
    }
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=451171&siteId=1