可在類文件中,右鍵->GO TO->Test 自動生成測試文件api
1.添加測試註解mvc
簡單方法測試maven
@RunWith(SpringRunner.class)
@SpringBootTest
public class GirlServiceTest {
@Autowired
private GirlService girlService;
@Test
public void findOne() throws Exception {
Girl girl = girlService.findOne(30);
Assert.assertEquals(new Integer(12),girl.getAge()); //斷言
}
}
restapi測試
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class GirlControllerTest {
@Autowired
private MockMvc mvc;
@Test
public void girlList() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/girls/list")).andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().string("sdf")); }}打包命令, cd girls mvn clean package //打包過程當中執行單元測試 mvn clean package -Dmaven.test.skip=true //打包過程當中,跳過單元測試