【发布时间】:2018-05-19 11:31:45
【问题描述】:
我在mockMVC中有如下测试方法
我需要改进测试以使其有意义。 目前测试不是测试,如何改进测试?只需添加数据合规性验证?如何添加它?数据验证验证的例子是什么? 如何测试UUID是否同意?
如何改进这些测试?
【问题讨论】:
标签: spring-boot junit mocking mockito mockmvc
我在mockMVC中有如下测试方法
我需要改进测试以使其有意义。 目前测试不是测试,如何改进测试?只需添加数据合规性验证?如何添加它?数据验证验证的例子是什么? 如何测试UUID是否同意?
如何改进这些测试?
【问题讨论】:
标签: spring-boot junit mocking mockito mockmvc
你可以这样做:
@Test
public void createUser() throws Exception {
User user = userRepository.save(new User("testname", "testemail", 10));
String userJson = toJson(user);
this.mockMvc.perform(post("/user/add/")
.contentType(contentType)
.content(userJson))
.andExpect(status().isCreated());
}
或
@Test
public void getUserById() throws Exception {
User user = userRepository.save(new User("testname", "testemail", 10));
mockMvc.perform(get("/user/" + user.getId()))
.andExpect(status().isOk())
.andExpect(content().contentType(contentType))
.andExpect(jsonPath("$.id", is(user.getId())))
.andExpect(jsonPath("$.username", is(user.getUsername())))
.andExpect(jsonPath("$.age", is(user.getAge())))
.andExpect(jsonPath("$.emailAddress", is(user.getEmailAddress())));
}
你可以在我的github repo查看整个测试类
希望对你有帮助
【讨论】: