为什么在MockMvcResultMatchers.model中Matchers.any(<t> .class)为null

时间:2018-07-23 17:52:09

标签: spring-mvc spring-mvc-test

我正在尝试验证模型的属性是否具有HostInterfaceDto的实例。运行测试时,出现断言错误,指出我提供的Matcher等于null。我非常确定我了解此doc。我究竟做错了什么?为什么会期望为空?

错误跟踪:

java.lang.AssertionError: Model attribute 'hostInterfaceDto' expected:<null> but was:<com.etisoftware.manager.beans.dto.hostinterfaces.HostInterfaceDto@4b14918a>

测试方法

    @Test
public void shouldGetAddPageForGet() throws Exception
{
    // Arrange
    Integer type = 1;

    List<String> versions = Arrays.asList("3.2.0", "3.2.1", "3.2.2", "3.2.3", "3.2.4", "3.2.5", "3.2.6", "3.2.7");
    Map<Integer, String> options = new HashMap<>();
    Mockito.when(hostInterfaceService.getVersions(type)).thenReturn(versions);
    Mockito.when(ovNodeService.getOptionList()).thenReturn(options);

    // Act
    mockMvc.perform(MockMvcRequestBuilders.get("/hostInterfaces/" + type + "/add"))
            .andExpect(MockMvcResultMatchers.status().isOk())
            .andExpect(MockMvcResultMatchers.view().name("hostInterfaces/add"))
            .andExpect(MockMvcResultMatchers.model().attribute("hostInterfaceDto", Matchers.any(
                    HostInterfaceDto.class)));

}

控制器方法

    @GetMapping("{hostInterfaceType}/add")
public String getAddPage(@PathVariable Integer hostInterfaceType, Model model)
{
    HostInterfaceDto dto = new HostInterfaceDto();
    dto.setType(hostInterfaceType);

    model.addAttribute("hostInterfaceDto", dto);
    addMappingSpecificAttributes(model, hostInterfaceType);
    return "hostInterfaces/add";
}

0 个答案:

没有答案