我在 Spring Boot 应用程序中的应用程序类中有一个逻辑,但我不知道如何进行单元和集成测试来覆盖它。
这是代码。
@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”运行的命令行应用程序
最佳答案
如果您使用Spring initializr ,将为您创建此测试。您可以将其称为集成测试,因为它将尝试启动您的应用程序上下文(从而集成其中的所有类)。事情是这样的:
@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...
}
}
对于您的特定域逻辑,您可以尝试断言是否创建了列表,但我会将其放在一个单独的类中作为单元测试。
关于java - Spring Boot,测试主应用程序类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51841014/