BookController { @PostMapping public ResponseEntity<Book> create(@RequestBody Book book, UriComponentsBuilder uriBuilder) { var created = bookService.create(book); var newBookUri = uriBuilder.path("/books/{isbn}").build(created.getIsbn()); return ResponseEntity .created(newBookUri) .body(created); } BookController(BookService bookService); @GetMapping @ResponseBody Iterable<Book> all(); @GetMapping("/{isbn}") ResponseEntity<Book> get(@PathVariable("isbn") String isbn); @PostMapping ResponseEntity<Book> create(@RequestBody Book book,
                                     UriComponentsBuilder uriBuilder); @GetMapping(produces = MediaType.TEXT_HTML_VALUE) String all(Model model); @GetMapping(params = "isbn", produces = MediaType.TEXT_HTML_VALUE) String get(@RequestParam("isbn") String isbn, Model model);  }
@Test public void shouldAddBook() throws Exception { when(bookService.create(any(Book.class))).thenReturn(new Book("123456789", "Test Book Stored", "T. Author")); mockMvc.perform(post("/templates/books") .contentType(MediaType.APPLICATION_JSON) .content("{ \"isbn\" : \"123456789\"}, \"title\" : \"Test Book\", \"authors\" : [\"T. Author\"]")) .andExpect(status().isCreated()) .andExpect(header().string("Location", "http: }