1. 程式人生 > >Spring Boot 進階之Web進階 學習 - 單元測試

Spring Boot 進階之Web進階 學習 - 單元測試

自動生成 添加 學習 one 類文件 dma AC mock ring

可在類文件中,右鍵->GO TO->Test 自動生成測試文件

1.添加測試註解

簡單方法測試

@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 //打包過程中,跳過單元測試

Spring Boot 進階之Web進階 學習 - 單元測試