0
votes

I know this question is an old one but I could not solve the problem in my case. When I am running test classes spring boot application is starting for each test class. I am having bellow annotations in my test class files. I want to start the application only once for all test classes

@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles("test")
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
@WithMockUser
2
Integration tests should load a context (and of course it takes time), but the loaded context is reused in other tests. Test more things in unit tests if possible - Sergii Zhevzhyk

2 Answers

2
votes

I have achieved this using a parent BaseTest

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
public abstract class BaseTest {

}

I use SpringRunner but SpringJUnit4ClassRunner should be ok too

0
votes
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)

By using the WebEnvironment.RANDOM_PORT you are telling spring boot to start a server on each class instantiation.

To avoid this, use a parent class that all testclasses extend from.