首页 文章

MockMVC测试如何验证答案

提问于
浏览
0

我在mockMVC中有以下测试方法

我需要改进测试才有意义 . 目前测试没有测试,如何改进测试?只需添加数据合规性验证?如何添加?什么可能是数据验证验证的例子?如何测试UUID是否同意?

如何改进这些测试?

1 回答

  • 0

    你可以这样做:

    @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中查看整个测试类

    希望这会对你有所帮助

相关问题