如何 JUnit 测试 Spring-Boot 的 Application.java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41775148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 06:08:46  来源:igfitidea点击:

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

javaspring-bootjunit

提问by 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?

道歉,因为这似乎是一种无用的行为,但有什么方法可以在 Spring-Boot(1.3.8.RELEASE) 的 Application.java 上实际进行 junit 测试,这个类除了启动 Spring-boot 什么都不做应用?

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!

也许我可以尝试捕捉异常?但是我还能测试什么,以便 JUnit 测试 SpringApplication.run()?任何例子表示赞赏。谢谢你们!

回答by

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:

但是,如果你想直接测试Application.main,你也可以这样测试:

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
        });
    }
}

回答by davidxxx

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!

也许我可以尝试捕捉异常?但是我还能测试什么,以便 JUnit 测试 SpringApplication.run()?任何例子表示赞赏。谢谢你们!

Making the test fail if an exception is caught is here helpless as a thrown exception during a test has already as consequence to make the test fail (in error more precisely).
If you execute already test classes that load the whole spring boot context (@SpringBootTest) you don't need to write a test to check that it is correctly loaded.

如果捕获到异常,则使测试失败在这里是无助的,因为在测试期间抛出的异常已经导致测试失败(更准确地说是错误)。
如果您已经执行了加载整个 Spring Boot 上下文 ( @SpringBootTest) 的测试类,则无需编写测试来检查它是否已正确加载。

You should test the Applicationclass if it has some logic.
For example you could declare a method annotated with @Beforethat does some processing after the context is loaded and you would like to assert that the processing is done as it should done. Other example : you could pass some arguments in the main()method and you would like to assert that the arguments are used by the method as expected.
All that is testable with @SpringBootTest.

Application如果它有一些逻辑,你应该测试这个类。
例如,您可以声明一个带有注释的方法,@Before该方法在加载上下文后执行一些处理,并且您想断言处理已按应完成的方式完成。其他示例:您可以在main()方法中传递一些参数,并且您想断言该方法按预期使用这些参数。
所有这些都可以用@SpringBootTest.

If your Applicationclass does nothing but :

如果您的Application课程仅执行以下操作:

 public static void main(String[] args) {                   
   SpringApplication.run(Application.class, args);         
 }

but you need all the same to cover the main()test method to be compliant with some coverage tool or organization, create an integration test for that, document it clearly and let the integration continuous does that because loading a Spring Boot application takes time. You can refer to this question.

但是您需要所有相同的内容来覆盖main()测试方法以符合某些覆盖工具或组织,为此创建一个集成测试,清楚地记录它并让集成持续这样做,因为加载 Spring Boot 应用程序需要时间。你可以参考这个问题