[英]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:您的测试代码可以这样做:
AuthOpsApplication.main()
.运行AuthOpsApplication.main()
中的内容。application.close()
to shut down.调用application.close()
关闭。Some general advice:一些一般性建议:
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 中,您将更加健壮。application
.问题中给出的代码不会初始化application
。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()
它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.