Commit 619ec32ca85b6d9e81c717c9e46ddeac1da9d9db
1 parent
537beae3
test: UI-Test der REST-Methode loginUser
Showing
2 changed files
with
127 additions
and
3 deletions
src/main/java/net/ziemers/swxercise/lg/user/dto/UserDto.java
| @@ -24,8 +24,18 @@ public class UserDto { | @@ -24,8 +24,18 @@ public class UserDto { | ||
| 24 | return username; | 24 | return username; |
| 25 | } | 25 | } |
| 26 | 26 | ||
| 27 | + public UserDto withUsername(final String username) { | ||
| 28 | + this.username = username; | ||
| 29 | + return this; | ||
| 30 | + } | ||
| 31 | + | ||
| 27 | public String getPassword() { return password; } | 32 | public String getPassword() { return password; } |
| 28 | 33 | ||
| 34 | + public UserDto withPassword(final String password) { | ||
| 35 | + this.password = password; | ||
| 36 | + return this; | ||
| 37 | + } | ||
| 38 | + | ||
| 29 | public String getFirstname() { | 39 | public String getFirstname() { |
| 30 | return firstname; | 40 | return firstname; |
| 31 | } | 41 | } |
src/test/java/net/ziemers/swxercise/ui/UserViewControllerTest.java
| 1 | package net.ziemers.swxercise.ui; | 1 | package net.ziemers.swxercise.ui; |
| 2 | 2 | ||
| 3 | -import org.junit.Assert; | 3 | +import net.ziemers.swxercise.lg.model.user.User; |
| 4 | +import net.ziemers.swxercise.lg.user.dto.UserDto; | ||
| 5 | +import net.ziemers.swxercise.lg.user.service.UserService; | ||
| 4 | import org.junit.Test; | 6 | import org.junit.Test; |
| 5 | import org.junit.runner.RunWith; | 7 | import org.junit.runner.RunWith; |
| 6 | import org.mockito.InjectMocks; | 8 | import org.mockito.InjectMocks; |
| 9 | +import org.mockito.Mock; | ||
| 7 | import org.mockito.junit.MockitoJUnitRunner; | 10 | import org.mockito.junit.MockitoJUnitRunner; |
| 8 | 11 | ||
| 12 | +import static org.junit.Assert.*; | ||
| 13 | +import static org.mockito.Mockito.*; | ||
| 14 | + | ||
| 9 | @RunWith(MockitoJUnitRunner.class) | 15 | @RunWith(MockitoJUnitRunner.class) |
| 10 | public class UserViewControllerTest { | 16 | public class UserViewControllerTest { |
| 11 | 17 | ||
| 12 | @InjectMocks | 18 | @InjectMocks |
| 13 | private UserViewController underTest; | 19 | private UserViewController underTest; |
| 14 | 20 | ||
| 21 | + @Mock | ||
| 22 | + private UserService userService; | ||
| 23 | + | ||
| 24 | + private UserDto userDto; | ||
| 25 | + | ||
| 26 | + private User user; | ||
| 27 | + | ||
| 28 | + private String actualString; | ||
| 29 | + | ||
| 30 | + @Test | ||
| 31 | + public void testJUnitFrameworkReturnsTrue() { | ||
| 32 | + assertTrue(true); | ||
| 33 | + } | ||
| 34 | + | ||
| 35 | + @Test(expected = AssertionError.class) | ||
| 36 | + public void testJUnitFrameworkThrowsException() { | ||
| 37 | + assertTrue(false); | ||
| 38 | + } | ||
| 39 | + | ||
| 15 | @Test | 40 | @Test |
| 16 | - public void test() { | ||
| 17 | - // TODO noch zu implementieren | 41 | + public void testGetAllUsersReturnsUsersJson() { |
| 42 | + // TODO Test ist noch zu implementieren | ||
| 43 | + } | ||
| 44 | + | ||
| 45 | + @Test | ||
| 46 | + public void testGetUserByIdReturnsUserJson() { | ||
| 47 | + // TODO Test ist noch zu implementieren | ||
| 48 | + } | ||
| 49 | + | ||
| 50 | + @Test | ||
| 51 | + public void testCreateUserReturnsSuccess() { | ||
| 52 | + // TODO Test ist noch zu implementieren | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | + @Test | ||
| 56 | + public void testUpdateUserReturnsSuccess() { | ||
| 57 | + // TODO Test ist noch zu implementieren | ||
| 58 | + } | ||
| 59 | + | ||
| 60 | + @Test | ||
| 61 | + public void testDeleteUserByIdReturnsSuccess() { | ||
| 62 | + // TODO Test ist noch zu implementieren | ||
| 63 | + } | ||
| 64 | + | ||
| 65 | + @Test | ||
| 66 | + public void testDeleteSessionUserReturnsSuccess() { | ||
| 67 | + // TODO Test ist noch zu implementieren | ||
| 68 | + } | ||
| 69 | + | ||
| 70 | + @Test | ||
| 71 | + public void testLoginUserReturnsSuccess() { | ||
| 72 | + | ||
| 73 | + given() | ||
| 74 | + .userDto() | ||
| 75 | + .user(); | ||
| 76 | + | ||
| 77 | + doing() | ||
| 78 | + .loginUser(); | ||
| 79 | + | ||
| 80 | + then() | ||
| 81 | + .assertLoginSuccess(); | ||
| 82 | + } | ||
| 83 | + | ||
| 84 | + @Test | ||
| 85 | + public void testLogoutUserReturnsSuccess() { | ||
| 86 | + // TODO Test ist noch zu implementieren | ||
| 87 | + } | ||
| 88 | + | ||
| 89 | + // given | ||
| 90 | + | ||
| 91 | + private UserViewControllerTest given() { | ||
| 92 | + return this; | ||
| 93 | + } | ||
| 94 | + | ||
| 95 | + private UserViewControllerTest userDto() { | ||
| 96 | + userDto = new UserDto() | ||
| 97 | + .withUsername("hbloed") | ||
| 98 | + .withPassword("secret"); | ||
| 99 | + | ||
| 100 | + return this; | ||
| 101 | + } | ||
| 102 | + | ||
| 103 | + private UserViewControllerTest user() { | ||
| 104 | + user = new User("Hein", "Blöd"); | ||
| 105 | + return this; | ||
| 106 | + } | ||
| 107 | + | ||
| 108 | + // doing | ||
| 109 | + | ||
| 110 | + private UserViewControllerTest doing() { | ||
| 111 | + return this; | ||
| 112 | + } | ||
| 113 | + | ||
| 114 | + private void loginUser() { | ||
| 115 | + // wir simulieren den Erfolg des userService (der hier nicht getestet werden soll), | ||
| 116 | + // sofern das korrekte userDto an ihn übergeben wurde | ||
| 117 | + when(userService.loginUser(userDto)).thenReturn(true); | ||
| 118 | + when(userService.getSessionUser()).thenReturn(user); | ||
| 119 | + | ||
| 120 | + actualString = underTest.loginUser(userDto); | ||
| 121 | + } | ||
| 122 | + | ||
| 123 | + // then | ||
| 124 | + | ||
| 125 | + private UserViewControllerTest then() { | ||
| 126 | + return this; | ||
| 127 | + } | ||
| 128 | + | ||
| 129 | + private void assertLoginSuccess() { | ||
| 130 | + final String expected = String.format("Ok (%s %s)", user.getFirstname(), user.getLastname()); | ||
| 131 | + assertEquals(expected, actualString); | ||
| 18 | } | 132 | } |
| 19 | 133 | ||
| 20 | } | 134 | } |