[英]How to verify that a spring-boot web server is running
I am writing an integration test for a spring-boot web server 我正在为Spring-boot Web服务器编写集成测试
I want to use junit-jupiter (junit5) to check if the server is running using Assumptions.assumeTrue
so that the test do not fail if the server is not running... 我想使用junit-jupiter(junit5)使用Assumptions.assumeTrue
检查服务器是否正在运行,以便在服务器未运行时测试不会失败...
There seems not to be any API support for such operation, but can it be achieved in some other way? 似乎没有任何API支持此类操作,但是可以通过其他方式实现吗? Pinging the server? ping服务器?
You could make use of the actuator
module in spring boot. 您可以在弹簧罩中使用actuator
模块。 It will provide you with a health check URL and you could make a call to this URL and verify that it returns healthy. 它将为您提供运行状况检查URL,您可以调用该URL并验证其是否恢复正常。
Now that that is out of the way, if you need the server to be running while running your tests, you should probably be using the spring boot test runner, instead of using the JUnit runner. 既然这样已经不成问题了,如果您需要在运行测试的同时运行服务器,则可能应该使用Spring Boot测试运行程序,而不是使用JUnit运行程序。 In this case you could still mock some of your spring beans and achieve the same kind of test, but with a spring context running alongside your tests. 在这种情况下,您仍然可以模拟一些Spring bean并完成相同的测试,但是在测试过程中同时运行spring上下文。 The downside of this is the overhead of needing to spin up the spring context before running tests, but if you need to use the actual web server, then this is what you should be doing. 这样做的缺点是在运行测试之前需要启动spring上下文的开销,但是如果您需要使用实际的Web服务器,那么这就是您应该做的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.