spring测试

今天在用spring做集成测试的时候,代码如下

 @Test
    @DatabaseSetup({"userData.xml", "emptyUserMailTemplateData.xml","systemMailTemplateData.xml"})
    @ExpectedDatabase(value = "saveUserMailTemplateExpected.xml", assertionMode = DatabaseAssertionMode.NON_STRICT)
    public void createNewTemplateShouldAddTemplateAndRedirectSuccessView() throws Exception {
        mockMvc.perform(post("/user/template/add").with(user(securityUser()))
                .param("name", "aaa")
                .param("approveStatus", "4")
                .param("template.subject", "test template")
                .param("template.body", "test template")
                .param("systemTemplateId",new String[]{"1"})
                .sessionAttr("template", new UserMailTemplate()))
                .andExpect(status().isFound())
                .andExpect(view().name("redirect:/user/template/create_or_update_success"));
    }

测试一直失败,可是仅看controller层和这里的测试方法是没有问题的,但是一直测试不成功,显示 expect1 actual 4,原来是

 @ExpectedDatabase

这个注释的原因,因为

saveUserMailTemplateExpected.xml里面的数据该字段为1,而我存的时候是设为4,好吧,忘了留意 那个注解了!其实这一整套测试还是很严谨的

原文地址:https://www.cnblogs.com/minjay/p/6649887.html