BookmarksController { @PostMapping @PreAuthorize("hasRole('ADMIN')") public ResponseEntity<Void> addBookmark(@RequestBody @Valid Bookmark bookmark){ UUID uuid = bookmarkService.addBookmark(bookmark); return ResponseEntity.created( BasicLinkBuilder.linkToCurrentMapping() .slash("bookmark") .slash(uuid) .toUri()) .build(); }  @PostMapping @PreAuthorize("hasRole('ADMIN')") ResponseEntity<Void> addBookmark(@RequestBody @Valid Bookmark bookmark); @GetMapping Resources<Resource<Bookmark>> findAllBookmarks(HttpServletRequest request);  }
@Test public void addABookmark() throws Exception { Bookmark value = new Bookmark("Packt publishing", "http: addBookmark(value); Mockito.verify(bookmarkService, atLeastOnce()).addBookmark(Mockito.any(Bookmark.class)); }
@Test public void addABookmarkFailsBecauseDescriptionIsNull() throws Exception { Bookmark value = new Bookmark(null, "http: mvc.perform( post("/bookmarks") .contentType(MediaType.APPLICATION_JSON_UTF8) .content(mapper.writeValueAsString(value)) .with(csrf()) ).andDo(print()) .andExpect(status().isBadRequest()) .andExpect(jsonPath("$[0].field").value("description")); Mockito.verify(bookmarkService, never()).addBookmark(Mockito.any(Bookmark.class)); }
@Test public void addABookmarkFailsBecauseUrlIsNull() throws Exception { Bookmark value = new Bookmark("testtest", "broken: mvc.perform( post("/bookmarks") .contentType(MediaType.APPLICATION_JSON_UTF8) .content(mapper.writeValueAsString(value)) .with(csrf()) ).andDo(print()) .andExpect(status().isBadRequest()) .andExpect(jsonPath("$[0].field").value("url")); Mockito.verify(bookmarkService, never()).addBookmark(Mockito.any(Bookmark.class)); }