使用 SpringRunner 加快 SpringBootTest 的启动时间
Speed up startup time of a SpringBootTest with SpringRunner
我正在寻找一种方法来最大程度地缩短 SpringBootTest
的启动时间,该 SpringBootTest
目前最多需要 15 秒才能启动并执行测试。我已经使用了模拟 webEnvironment
和特定 RestController
class 的 standaloneSetup()
。
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());
}
}
我应该使用任何其他配置来加快速度吗?我使用 Spring Boot 1.5.9.
因为您正在测试特定的控制器。因此,您可以通过使用 @WebMvcTest
注释而不是通用测试注释 @SpringBootTest
来更细化。它会快得多,因为它只会加载您的应用程序的一部分。
@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());
}
}
我正在寻找一种方法来最大程度地缩短 SpringBootTest
的启动时间,该 SpringBootTest
目前最多需要 15 秒才能启动并执行测试。我已经使用了模拟 webEnvironment
和特定 RestController
class 的 standaloneSetup()
。
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());
}
}
我应该使用任何其他配置来加快速度吗?我使用 Spring Boot 1.5.9.
因为您正在测试特定的控制器。因此,您可以通过使用 @WebMvcTest
注释而不是通用测试注释 @SpringBootTest
来更细化。它会快得多,因为它只会加载您的应用程序的一部分。
@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());
}
}