我正在寻找一种方法来最小化SpringBootTest的启动时间,目前它在启动和执行测试之前需要15秒。我已经使用了模拟的webEnvironment和特定RestController类的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。
发布于 2018-01-19 02:58:16
因为您正在测试一个特定的控制器。因此,您可以通过使用@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());
}
}https://stackoverflow.com/questions/48328252
复制相似问题