dimanche 13 octobre 2019

Difference between webEnvironment = RANDOM_PORT and webEnvironment = MOCK

I wrote spring boot integration test and it is working. Here is the test config:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT)
@AutoConfigureMockMvc
@Transactional
public class SomeTest {
   @Autowired
   private MockMvc mvc;

   @Test
   public void insertEventTest(){
      ...testing something...
   }

}

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 ''

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 ?

Aucun commentaire:

Enregistrer un commentaire