本文介绍了webEnvironment = RANDOM_PORT和webEnvironment = MOCK之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我写了spring boot集成测试,它正在运行.这是测试配置:I wrote spring boot integration test and it is working. Here is the test config:@RunWith(SpringRunner.class)@SpringBootTest(webEnvironment = RANDOM_PORT)@AutoConfigureMockMvc@Transactionalpublic class SomeTest { @Autowired private MockMvc mvc; @Test public void insertEventTest(){ ...testing something... }}我了解到,在设置 webEnvironment = RANDOM_PORT 时,spring将初始化嵌入式Web服务器并对该Web服务器运行此测试.我在运行此测试时查看了日志,并看到嵌入式 TomcatWebServer 已启动.初始化Tomcat大约需要6秒钟,但是在日志的这两部分之间,很少初始化其他bean,因此我很确定初始化Tomcat并不是6秒钟,而是少于6秒钟.日志的一部分:I understand that when setting webEnvironment = RANDOM_PORT spring will initialize an embedded web server and run this test against that web server. I take a look at logs when running this test and saw that embedded TomcatWebServer was started. It takes about 6 seconds to initialize Tomcat but between those two parts of the logs few other beans were initialized so I am pretty sure that initializing Tomcat was not 6 seconds but less than 6 seconds.One part of the logs:2019-10-13 16:03:20.065 INFO 8596 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 0 (http)2019-10-13 16:03:20.098 INFO 8596 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]2019-10-13 16:03:20.098 INFO 8596 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.14]2019-10-13 16:03:20.108 INFO 8596 --- [ main] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib:/usr/lib64:/lib64:/lib:/usr/lib]2019-10-13 16:03:20.228 INFO 8596 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext ...更多日志,然后最终...some more logs and then finally 2019-10-13 16:03:26.366 INFO 8596 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 38335 (http) with context path ''我运行了3次测试,完成测试需要12、11.4和12秒.之后,我尝试设置 @SpringBootTest(webEnvironment = MOCK).我注意到这一次 Tomcat 尚未初始化(Spring嘲笑了Web服务器).执行时间为11.3、11和10.8秒.在这两种情况下,所有测试均为绿色.我的想法是,我将使用模拟的Web服务器提高测试性能,但得到的结果是1秒.如果我们牢记我的应用程序上下文在测试类之间进行了缓存,那么我基本上一无所获.所以我的问题是,在哪种情况下,测试将通过 @SpringBootTest(webEnvironment = RANDOM_PORT)通过,而通过 @SpringBootTest(webEnvironment = MOCK)失败,反之亦然,何时应该使用 RANDOM_PORT 和 MOCK 吗?I run test 3 times and it takes 12 ,11.4 and 12 seconds for test to complete. After that, I tried to set @SpringBootTest(webEnvironment = MOCK) . I noticed that this time Tomcat was not initialized(web server was mocked by spring). Execution times were 11.3, 11 and 10.8 seconds. In both cases, all tests were green. My thoughts were that I will improve performance of my tests with mocked web server but what I got is 1 second. If we have in mind that my application context is cached between test classes, I basically got nothing. So my question is, in which cases test will pass with @SpringBootTest(webEnvironment = RANDOM_PORT) and fail with @SpringBootTest(webEnvironment = MOCK) or vice versa and when I should use RANDOM_PORT and when MOCK ?推荐答案使用 @SpringBootTest(webEnvironment = WebEnvironment.MOCK)加载Web应用程序上下文并提供模拟Web环境.它不会加载真正的http服务器,只是模拟了整个网络服务器的行为.Using @SpringBootTest(webEnvironment = WebEnvironment.MOCK) loads a web application context and provides a mock web environment. It doesn’t load a real http server, just mocks the entire web server behavior. WebEnvironment.MOCK 为您提供了一些优点,例如易于使用或隔离其他因素,但这可能不是一个很好的集成测试方法.WebEnvironment.MOCK gives you some advantages like ease of use or isolation of other factors but it might not be a good integration test practice.集成测试应与生产环境尽可能相似.考虑到这一点,使用 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)是一个更好的选择.这种方法更接近于测试实际应用程序.您可以看到整个系统是否可以按预期工作.Integration tests should be as similar as possible to the production environment. Considering this, using @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) would be a better choice. This approach is closer to test the real application. You can see whether the whole system is going to work as expected.当您使用 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)时,您将使用真实的http服务器进行测试.在这种情况下,您需要使用 TestRestTemplate .当您要测试与Web层相关的一些周围行为时,这很有用.When you use @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) you test with a real http server. In this case, you need to use a TestRestTemplate. This is helpful when you want to test some surrounding behavior related to the web layer. 这篇关于webEnvironment = RANDOM_PORT和webEnvironment = MOCK之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-17 03:47