@Test public void shouldReturnNoBookWhenNotFound() throws Exception { when(bookService.find(anyString())).thenReturn(Optional.empty()); mockMvc.perform(get("/books.html").param("isbn", "123")) .andExpect(status().isOk()) .andExpect(view().name("books/details")) .andExpect(model().attributeDoesNotExist("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); }
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);  }