From a34be087d9e2c7ec2789624d6910e2dcea3f8eeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Horacio=20L=C3=B3pez?= Date: Tue, 17 Nov 2020 20:06:51 -0300 Subject: [PATCH 1/4] Update UserController.java --- .../baeldung/crud/controllers/UserController.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/spring-boot-modules/spring-boot-crud/src/main/java/com/baeldung/crud/controllers/UserController.java b/spring-boot-modules/spring-boot-crud/src/main/java/com/baeldung/crud/controllers/UserController.java index 8a7ef96f1e..4fac1ba0ca 100644 --- a/spring-boot-modules/spring-boot-crud/src/main/java/com/baeldung/crud/controllers/UserController.java +++ b/spring-boot-modules/spring-boot-crud/src/main/java/com/baeldung/crud/controllers/UserController.java @@ -23,6 +23,12 @@ public class UserController { this.userRepository = userRepository; } + @GetMapping("/index") + public String userList(User user) { + model.addAttribute("users", userRepository.findAll()); + return "index"; + } + @GetMapping("/signup") public String showSignUpForm(User user) { return "add-user"; @@ -35,7 +41,6 @@ public class UserController { } userRepository.save(user); - model.addAttribute("users", userRepository.findAll()); return "redirect:/index"; } @@ -43,6 +48,7 @@ public class UserController { public String showUpdateForm(@PathVariable("id") long id, Model model) { User user = userRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("Invalid user Id:" + id)); model.addAttribute("user", user); + return "update-user"; } @@ -54,7 +60,7 @@ public class UserController { } userRepository.save(user); - model.addAttribute("users", userRepository.findAll()); + return "redirect:/index"; } @@ -62,7 +68,7 @@ public class UserController { public String deleteUser(@PathVariable("id") long id, Model model) { User user = userRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("Invalid user Id:" + id)); userRepository.delete(user); - model.addAttribute("users", userRepository.findAll()); - return "index"; + + return "redirect:/index"; } } From 34243ccef424815e1d28f346cc00f8695d9e557e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Horacio=20L=C3=B3pez?= Date: Tue, 17 Nov 2020 20:16:10 -0300 Subject: [PATCH 2/4] Removed user param on userList method --- .../main/java/com/baeldung/crud/controllers/UserController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-boot-modules/spring-boot-crud/src/main/java/com/baeldung/crud/controllers/UserController.java b/spring-boot-modules/spring-boot-crud/src/main/java/com/baeldung/crud/controllers/UserController.java index 4fac1ba0ca..9c27c1764c 100644 --- a/spring-boot-modules/spring-boot-crud/src/main/java/com/baeldung/crud/controllers/UserController.java +++ b/spring-boot-modules/spring-boot-crud/src/main/java/com/baeldung/crud/controllers/UserController.java @@ -24,7 +24,7 @@ public class UserController { } @GetMapping("/index") - public String userList(User user) { + public String userList() { model.addAttribute("users", userRepository.findAll()); return "index"; } From 01ecd328687ff1465e5e3688422011b24273086f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Horacio=20L=C3=B3pez?= Date: Tue, 17 Nov 2020 21:50:02 -0300 Subject: [PATCH 3/4] Fixed test --- .../src/test/java/com/baeldung/crud/UserControllerUnitTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-boot-modules/spring-boot-crud/src/test/java/com/baeldung/crud/UserControllerUnitTest.java b/spring-boot-modules/spring-boot-crud/src/test/java/com/baeldung/crud/UserControllerUnitTest.java index f1455f7a73..651f17b57a 100644 --- a/spring-boot-modules/spring-boot-crud/src/test/java/com/baeldung/crud/UserControllerUnitTest.java +++ b/spring-boot-modules/spring-boot-crud/src/test/java/com/baeldung/crud/UserControllerUnitTest.java @@ -78,6 +78,6 @@ public class UserControllerUnitTest { @Test(expected = IllegalArgumentException.class) public void whenCalleddeleteUser_thenIllegalArgumentException() { - assertThat(userController.deleteUser(1l, mockedModel)).isEqualTo("index"); + assertThat(userController.deleteUser(1l, mockedModel)).isEqualTo("redirect:/index"); } } From d2f2bb6ab0b009c8e3e1939ffa3d215c9e98416b Mon Sep 17 00:00:00 2001 From: Horacio Lopez Date: Tue, 17 Nov 2020 21:57:15 -0300 Subject: [PATCH 4/4] Added test and some fixes --- .../java/com/baeldung/crud/controllers/UserController.java | 2 +- .../test/java/com/baeldung/crud/UserControllerUnitTest.java | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/spring-boot-modules/spring-boot-crud/src/main/java/com/baeldung/crud/controllers/UserController.java b/spring-boot-modules/spring-boot-crud/src/main/java/com/baeldung/crud/controllers/UserController.java index 9c27c1764c..fb86683d6b 100644 --- a/spring-boot-modules/spring-boot-crud/src/main/java/com/baeldung/crud/controllers/UserController.java +++ b/spring-boot-modules/spring-boot-crud/src/main/java/com/baeldung/crud/controllers/UserController.java @@ -24,7 +24,7 @@ public class UserController { } @GetMapping("/index") - public String userList() { + public String showUserList(Model model) { model.addAttribute("users", userRepository.findAll()); return "index"; } diff --git a/spring-boot-modules/spring-boot-crud/src/test/java/com/baeldung/crud/UserControllerUnitTest.java b/spring-boot-modules/spring-boot-crud/src/test/java/com/baeldung/crud/UserControllerUnitTest.java index 651f17b57a..77d83698de 100644 --- a/spring-boot-modules/spring-boot-crud/src/test/java/com/baeldung/crud/UserControllerUnitTest.java +++ b/spring-boot-modules/spring-boot-crud/src/test/java/com/baeldung/crud/UserControllerUnitTest.java @@ -28,6 +28,11 @@ public class UserControllerUnitTest { userController = new UserController(mockedUserRepository); } + @Test + public void whenCalledIndex_thenCorrect() { + assertThat(userController.showUserList(mockedModel)).isEqualTo("index"); + } + @Test public void whenCalledshowSignUpForm_thenCorrect() { User user = new User("John", "john@domain.com");