@Test public void shouldReturnBookWhenFound() throws Exception { Book book = new Book("123", "Spring 5 Recipes", "Marten Deinum", "Josh Long"); when(bookService.find(anyString())).thenReturn( Optional.of(book)); mockMvc.perform(get("/books.html").param("isbn", "123")) .andExpect(status().isOk()) .andExpect(view().name("books/details")) .andExpect(model().attribute("book", Matchers.is(book))); }
@GetMapping(value = "/books.html", params = "isbn") public String get(@RequestParam("isbn") String isbn, Model model) { bookService.find(isbn) .ifPresent(book -> model.addAttribute("book", book)); return "books/details"; }
BookController { @GetMapping(value = "/books.html", params = "isbn") public String get(@RequestParam("isbn") String isbn, Model model) { bookService.find(isbn) .ifPresent(book -> model.addAttribute("book", book)); return "books/details"; } }
BookController { @GetMapping(value = "/books.html", params = "isbn") public String get(@RequestParam("isbn") String isbn, Model model) { bookService.find(isbn) .ifPresent(book -> model.addAttribute("book", book)); return "books/details"; } BookController(BookService bookService); }
BookController { @GetMapping(value = "/books.html", params = "isbn") public String get(@RequestParam("isbn") String isbn, Model model) { bookService.find(isbn) .ifPresent(book -> model.addAttribute("book", book)); return "books/details"; } BookController(BookService bookService); @GetMapping("/books.html") String all(Model model); @GetMapping(value = "/books.html", params = "isbn") String get(@RequestParam("isbn") String isbn, Model model); @PostMapping("/books") Book create(@ModelAttribute Book book); @GetMapping("/books/404") ResponseEntity notFound(); @GetMapping("/books/400") ResponseEntity foo(); @GetMapping("/books/500") void error(); }
BookController { @GetMapping(value = "/books.html", params = "isbn") public String get(@RequestParam("isbn") String isbn, Model model) { bookService.find(isbn) .ifPresent(book -> model.addAttribute("book", book)); return "books/details"; } BookController(BookService bookService); @GetMapping("/books.html") String all(Model model); @GetMapping(value = "/books.html", params = "isbn") String get(@RequestParam("isbn") String isbn, Model model); @PostMapping("/books") Book create(@ModelAttribute Book book); @GetMapping("/books/404") ResponseEntity notFound(); @GetMapping("/books/400") ResponseEntity foo(); @GetMapping("/books/500") void error();  }