重点注意下,@RunWith(SpringRunner.class)这个注释一定要有,因为调用controller方法需要重启一个controller,要运行,就需要一个端口,这个可以随机开启一个端口供测试用。
@RunWith(SpringRunner.class)
@SpringBootTest
@WebAppConfiguration
@AutoConfigureMockMvc
public class ProjectRecipeControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
public void save() throws Exception{
ProjectRecipeVo vo = new ProjectRecipeVo();
dto.setPatientNumber("1234564564");
dto.setPatientId("YYHZ20190612LEEB");
MvcResult mvcResult=mockMvc.perform(MockMvcRequestBuilders.post("/projectRecipe/save")
.content(FastJsonUtil.toJSONString(vo)))
.andReturn();
int status=mvcResult.getResponse().getStatus();
String content =mvcResult.getResponse().getContentAsString();
}
}