Spring Boot 进阶之Web进阶 学习 - 单元测试

可在类文件中,右键->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 //打包过程中,跳过单元测试
posted @ 2018-04-25 17:29  zhcnblog  阅读(181)  评论(0编辑  收藏  举报