如何JUnit测试Spring-Boot的Application.java [英] How to JUnit Test Spring-Boot's Application.java
问题描述
道歉,因为这似乎是一个无用的行为,但有没有办法我们可以在Spring-Boot(1.3.8.RELEASE)的Application.java上实际进行junit测试,这个类除了启动一个Spring-boot应用程序?
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?
如下所示:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
try{
SpringApplication.run(Application.class, args);
}catch(Exception e){
//code here
}
}
}
也许我可以尝试捕捉异常?但我还有什么可以测试,以便JUnit将测试SpringApplication.run()?任何例子都表示赞赏。谢谢大家!
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!
推荐答案
通常,以下内容足以测试应用程序上下文是否启动。
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() {
}
}
但是,如果你想直接测试 Application.main
,你也可以这样测试:
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
});
}
}
这篇关于如何JUnit测试Spring-Boot的Application.java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!