当前测试显示返回的两个对象相同,但是断言失败。有什么可以比较的吗?
@Test
public void test_search() throws Exception {
TestObject testObject= createTestObject();
ModelAndView expectedReturn = new ModelAndView("example/test", "testForm", testObject);
expectedReturn.addObject("testForm", testObject);
ModelAndView actualReturn = testController.search(testObject);
assertEquals("Model and View objects do not match", expectedReturn, actualReturn);
}
答案 0 :(得分:0)
我建议您编写一个真正的Spring MVC测试。
例如就像我用弹簧靴做的一样
@AutoConfigureMockMvc
@SpringBootTest(classes = {YourSpringBootApplication.class})
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
@RunWith(SpringRunner.class)
public class RestControllerTest {
@Autowired
private MockMvc mvc;
@org.junit.Test
public void test_search() throws Exception {
TestObject testObject= createTestObject();
mvc.perform(MockMvcRequestBuilders
.get("/yourRestEndpointUri"))
.andExpect(model().size(1))
.andExpect(model().attribute("testObject", testObject))
.andExpect(status().isOk());
}
}
重要的是使用org.springframework.test.web.servlet.result.ModelResultMatchers.model()
方法检查模型属性(在示例中为静态导入)