[英]How to test nested Object in with Spring MockMVC?
I have a controller with a POST method to test. 我有一个带有POST方法进行测试的控制器。 The method will post a book to the reader's reading list.
该方法会将一本书发布到读者的阅读列表中。 It will grab the currently authenticated user as the reader.
它将把当前经过身份验证的用户作为阅读器。 The
Book
Class has a field that is a Reader
type. Book
Class的字段为Reader
类型。 The following code uses contains(samePropertyValuesAs(expectedBook))
, but the reader
instance location is different from the expectedReader
I created obviously. 下面的代码使用
contains(samePropertyValuesAs(expectedBook))
但reader
实例位置是不同的expectedReader
我明明创建。
What is the correct way to test such nested Object in MockMVC? 在MockMVC中测试此类嵌套对象的正确方法是什么?
@Test
@WithUserDetails("z")
public void addBookShouldShowTheBookInReadingList() throws Exception {
final String bookTitle = "BOOK A";
final String bookAuthor = "JK";
final String isbn = "1234567890";
final String description = "Yet another book.";
mockMvc.perform(post("/readingList")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.param("title", bookTitle)
.param("author", bookAuthor)
.param("isbn", isbn)
.param("description", description)
.with(csrf()))
.andExpect(status().is3xxRedirection())
.andExpect(header().string("Location", "/readingList"));
Reader expectedReader = new Reader();
expectedReader.setUsername("z");
expectedReader.setPassword("test");
expectedReader.setFullName("Z H");
Book expectedBook = new Book();
expectedBook.setId(1L);
expectedBook.setTitle(bookTitle);
expectedBook.setAuthor(bookAuthor);
expectedBook.setIsbn(isbn);
expectedBook.setDescription(description);
expectedBook.setReader(expectedReader);
mockMvc.perform(get("/readingList"))
.andExpect(status().isOk())
.andExpect(model().attributeExists("books"))
.andExpect(model().attribute("books", hasSize(1)))
.andExpect(model().attribute("books", contains(samePropertyValuesAs(expectedBook))));
}
Update - additional POST signature. 更新-其他POST签名。
@Controller
@RequestMapping(value = "/readingList")
public class ReadingListController {
private ReadingListService readingListService;
private VersionConfig versionConfig;
@Autowired
public ReadingListController(final ReadingListService readingListService,
final VersionConfig versionConfig) {
this.readingListService = readingListService;
this.versionConfig = versionConfig;
}
@RequestMapping(value = "", method = RequestMethod.GET)
public String readersBooks(Reader reader, Model model) {
readingListService.findByReader(reader)
.ifPresent(books -> {
model.addAttribute("books", books);
model.addAttribute("reader", reader);
model.addAttribute("releaseId", versionConfig.getReleaseId());
});
return "readingList";
}
@RequestMapping(value = "", method = RequestMethod.POST)
public String addToReadingList(Reader reader, Book book) {
book.setReader(reader);
readingListService.save(book);
return "redirect:/readingList";
}
}
So there is a hasToString()
matcher could be used in hamcrast to get this done. 因此,可以在Hamcrast中使用
hasToString()
匹配器来完成此操作。
mockMvc.perform(get("/readingList"))
.andExpect(status().isOk())
.andExpect(model().attributeExists("books"))
.andExpect(model().attribute("books", hasSize(1)))
.andExpect(model().attribute("books", contains(hasToString(expectedBook.toString()))));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.