Spring Boot,测试主应用程序类 [英] Spring Boot, test main application class

查看:71
本文介绍了Spring Boot,测试主应用程序类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Spring Boot应用程序的应用程序类中有一个逻辑,但是我不知道如何做一个单元和一个集成测试来覆盖它.

I have a logic in my application class in a Spring Boot app, but I don't know how to do an unit and an integration test to cover it.

这是代码.

    @SpringBootApplication
    public class MlgApplication {

        public static void main(String[] args) throws IOException {
            ConfigurableApplicationContext run = SpringApplication.run(MlgApplication.class, args);

            ListBean ListBean = run.getBean(ListBean.class);
            ListBean.createList();
        }
    }

这是一个与'java -jar mlg.jar'一起运行的命令行应用程序

It's a command line application that runs with 'java -jar mlg.jar'

推荐答案

如果您使用的是 Spring initializr ,将为您创建该测试.您可以将其称为集成测试,因为它会尝试启动您的应用程序上下文(因此将所有类集成在一起).它是这样的:

If you are using Spring initializr, this test will be created for you. You may call it an integration test because it will try to start your application context (thus integrating all classes inisde it). It goes something like this:

@RunWith(SpringRunner.class)
@SpringBootTest
public class BootApplicationTests {

    @Test
    public void contextLoads() {
    // some more optional integration assertions here
    // like asserting number of beans, are they null, etc...
    }
}

对于您的特定域逻辑,您可以尝试断言是否创建了列表,但我会将其作为单元测试放在单独的类中.

And for your specific domain logic, you can try to assert if the list is created but I would put that in a separate class as a unit test.

这篇关于Spring Boot,测试主应用程序类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆