Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Speed up startup time of a SpringBootTest with SpringRunner

Tags:

I am looking for a way to minimize startup time of a SpringBootTest which currently takes up to 15 seconds until it is started and tests are executed. I already use the mocked webEnvironment and the standaloneSetup() of the specific RestController class.

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.MOCK;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = MOCK)
public class DataControllerMvcTests {

    @Autowired
    private DataService dataService;

    @Autowired
    private DataController dataController;

    private MockMvc mockMvc;

    @Before
    public void setup() {
        mockMvc = MockMvcBuilders
                .standaloneSetup(dataController)
                .build();
    }

    @Test
    @WithMockUser(roles = "READ_DATA")
    public void readData() throws Exception {
        mockMvc.perform(get("/data")).andExpect(status().is2xxSuccessful());
    }
}

Is there any other configuration I should use in order to speed it up? I use Spring Boot 1.5.9.

like image 324
user3105453 Avatar asked Jan 18 '18 18:01

user3105453


People also ask

Why does Spring Boot take so long to start?

When a Spring Boot Application has a slow startup, it can be one or more beans and related dependencies taking longer to initialise and slowing down the entire process. Profiling the Spring Boot application doesn't often help in diagnosing startup issues.

What does SpringBootTest annotation do?

The @SpringBootTest annotation is useful when we need to bootstrap the entire container. The annotation works by creating the ApplicationContext that will be utilized in our tests. We can use the webEnvironment attribute of @SpringBootTest to configure our runtime environment; we're using WebEnvironment.

What does @RunWith SpringRunner class mean?

@RunWith(SpringRunner. class) tells JUnit to run using Spring's testing support. SpringRunner is the new name for SpringJUnit4ClassRunner , it's just a bit easier on the eye. @SpringBootTest is saying “bootstrap with Spring Boot's support” (e.g. load application.


1 Answers

Since you are testing a particular controller. So you can be more granular by using the @WebMvcTest annotation instead of the general test annotation @SpringBootTest. It will be much faster as it will only load a slice of your app.

@RunWith(SpringRunner.class)
@WebMvcTest(value = DataController.class)
public class DataControllerMvcTests {

    @Mock
    private DataService dataService;

    @Autowired
    private MockMvc mockMvc;

    @Before
    public void setup() {
        mockMvc = MockMvcBuilders
                .standaloneSetup(dataController)
                .build();
    }

    @Test
    public void readData() throws Exception {
        //arrange mock data
        //given( dataService.getSomething( "param1") ).willReturn( someData );

        mockMvc.perform(get("/data")).andExpect(status().is2xxSuccessful());
    }
}
like image 126
alltej Avatar answered Sep 19 '22 12:09

alltej