简体   繁体   English

测试 Spring boot 使用 Junit 启动应用程序

[英]Test Spring boot Application startup using Junit

I have some business logic that runs when Spring boot application starts (ie in main method)我有一些在 Spring 启动应用程序启动时运行的业务逻辑(即在 main 方法中)

@SpringBootApplication
@EnableGatewayService
@EnableAsync
@Slf4j
public class AuthOpsApplication {
     public static void main(String[] args) throws IOException {
        application.addListeners(new WEPFallbackListener());
        application.addListeners(new SaeListener());
        application.run(args);
        // some business logic
        log.info("logs some info of the business logic");

    }
}

I want to write a test case to check if the log.info is getting printed and its value is as expected.我想编写一个测试用例来检查 log.info 是否被打印并且它的值是否符合预期。 I do not want to test the business logic as a standalone code, I would like to run the main function and somehow read the log that it puts out, Is this possible?我不想将业务逻辑作为独立代码进行测试,我想运行主 function 并以某种方式读取它输出的日志,这可能吗?

What I tried:我尝试了什么:

import org.springframework.test.context.junit4.SpringRunner;
import uk.org.lidalia.slf4jtest.TestLogger;
import uk.org.lidalia.slf4jtest.TestLoggerFactory;
import org.springframework.boot.test.autoconfigure.actuate.metrics.AutoConfigureMetrics;
import org.springframework.test.context.ActiveProfiles;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = AuthOpsApplication.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.ANY)
@ActiveProfiles("test")
@AutoConfigureMetrics
public class LogTest {
    TestLogger logger = TestLoggerFactory.getTestLogger(AuthOpsApplication.class);


    @Test
    public void testAnyLog() throws IOException {
        System.out.println(logger.getLoggingEvents());

    }

    @After
    public void clearLoggers() {
        TestLoggerFactory.clear();
    }
}

This runs successfully and creates all the Beans but does not actually call the main() method of my AuthOpsApplication class.这成功运行并创建了所有 Bean,但实际上并未调用我的 AuthOpsApplication class 的 main() 方法。

( I can say this because the System.out.println() that I did gives all the other logs except the ones in main() method). (我可以这样说,因为我所做的 System.out.println() 提供了所有其他日志,除了 main() 方法中的日志)。

Thanks in advance for the help.在此先感谢您的帮助。

Your test code can do this:您的测试代码可以这样做:

  1. Apply https://stackoverflow.com/a/1119559/6944068 to capture stdout.应用https://stackoverflow.com/a/1119559/6944068捕获标准输出。
  2. Run what's inside AuthOpsApplication.main() .运行AuthOpsApplication.main()中的内容。
  3. Collect captured stdout until you see the expected message.收集捕获的标准输出,直到您看到预期的消息。
  4. Call application.close() to shut down.调用application.close()关闭。

Some general advice:一些一般性建议:

  • It's pretty common to do startup logging right after calling application.run , but strictly speaking that's a race condition and you don't known at what point the business info you're logging will be available.在调用application.run之后立即进行启动日志记录是很常见的,但严格来说这是一个竞争条件,您不知道您正在记录的业务信息何时可用。 You'll be more robust if you put the logging into the beans that have that information.如果您将日志记录放入具有该信息的 bean 中,您将更加健壮。
  • The code as given in the question does not initialize application .问题中给出的代码不会初始化application
  • You don't want to copy the code for step 2 from main() , you'll want to put that into a function you can call from your test code.您不想从main()复制步骤 2 的代码,您需要将其放入可以从测试代码调用的 function 中。 That function should also return application so the test code can close() it in step 4. function 也应该返回application ,以便测试代码可以在步骤 4 中close()它。
  • If you have more tests that require the full application, you'll want to organize them that you have application startup and shutdown only once.如果您有更多需要完整应用程序的测试,您将希望将它们组织为您只启动和关闭应用程序一次。 (I don't have recipes for that, how to do that would be food for another SO question.) (我没有这方面的食谱,如何做到这一点将是另一个 SO 问题的食物。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 junit 测试将命令行参数传递给 Spring Boot 应用程序 - Using junit test to pass command line argument to Spring Boot application 如何使用 JUnit 在没有 @SpringBootApplication 的情况下测试 Java Spring Boot 应用程序? - How to test Java Spring Boot application without @SpringBootApplication using JUnit? 使用 Junit 5 的 Spring Boot Cucumber 测试不会加载 application-test.properties - Spring Boot Cucumber Test with Junit 5 will not load application-test.properties 在 JUnit 测试类中使用属性值而不加载整个 Spring 引导应用程序上下文 - Using property values in JUnit test classes without load whole Spring Boot application context Spring boot junit 测试加载应用程序上下文失败 - Spring boot junit test failed loading application context Spring Boot 访问 application.properties 以进行 JUnit 测试 - Spring Boot accessing application.properties for JUnit Test Spring 引导控制台应用程序导致 JUnit 测试用例等待 - Spring Boot console application causes JUnit test case to wait 在Junit测试中无法覆盖Spring-Boot应用程序属性 - Cannot overrid Spring-Boot application properties in Junit Test 为具有自动装配组件的Spring Boot Service应用程序编写JUnit测试用例 - Writing JUnit test cases for a Spring Boot Service Application with autowired components 从Junit Test运行时,Spring Boot Application引发异常 - Spring Boot Application is throwing exception when runs from Junit Test
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM