Steffen Nitschke
4 years ago
2 changed files with 91 additions and 13 deletions
-
34fh.fd.ci.server/src/main/java/de/fd/fh/server/user/web/UserController.java
-
70fh.fd.ci.server/src/test/java/de/fd/fh/server/user/web/UserControllerTest.java
@ -0,0 +1,70 @@ |
|||||
|
package de.fd.fh.server.user.web; |
||||
|
|
||||
|
import com.fasterxml.jackson.core.JsonProcessingException; |
||||
|
import de.fd.fh.server.user.UserService; |
||||
|
import org.junit.jupiter.api.BeforeEach; |
||||
|
import org.junit.jupiter.api.Test; |
||||
|
import org.mockito.ArgumentCaptor; |
||||
|
import org.mockito.Mock; |
||||
|
import org.mockito.MockitoAnnotations; |
||||
|
import spark.Request; |
||||
|
import spark.Response; |
||||
|
import spark.Session; |
||||
|
|
||||
|
import static org.junit.jupiter.api.Assertions.*; |
||||
|
import static org.mockito.ArgumentMatchers.any; |
||||
|
import static org.mockito.BDDMockito.then; |
||||
|
import static org.mockito.Mockito.verify; |
||||
|
import static org.mockito.Mockito.when; |
||||
|
|
||||
|
class UserControllerTest |
||||
|
{ |
||||
|
@Mock |
||||
|
Request request; |
||||
|
@Mock |
||||
|
Response response; |
||||
|
@Mock |
||||
|
UserService service; |
||||
|
|
||||
|
@BeforeEach |
||||
|
void before() |
||||
|
{ |
||||
|
MockitoAnnotations.openMocks(this); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
void getUserTest() throws JsonProcessingException |
||||
|
{ |
||||
|
when(request.params(any())).thenReturn("12345"); |
||||
|
when(service.getSmallPlayer(any())).thenReturn(new UserRequest("12345", "TestName")); |
||||
|
|
||||
|
final ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); |
||||
|
|
||||
|
final Response result = new UserController(service).getUser(request, response); |
||||
|
|
||||
|
verify(response).body(captor.capture()); |
||||
|
|
||||
|
assertEquals(response, result, "Should return the incomming response."); |
||||
|
assertEquals("{\"id\":\"12345\",\"name\":\"TestName\"}", captor.getValue(), |
||||
|
"Should return UserRequest in Body."); |
||||
|
then(service).should().getSmallPlayer(any()); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
void failedGetUserTest() throws JsonProcessingException |
||||
|
{ |
||||
|
when(request.params(any())).thenReturn("12345"); |
||||
|
when(service.getSmallPlayer(any())).thenReturn(null); |
||||
|
|
||||
|
final ArgumentCaptor<Integer> statusCaptor = ArgumentCaptor.forClass(Integer.class); |
||||
|
|
||||
|
final Response result = new UserController(service).getUser(request, response); |
||||
|
|
||||
|
verify(response).status(statusCaptor.capture()); |
||||
|
|
||||
|
assertEquals(response, result, "Should return the incomming response."); |
||||
|
assertEquals(Integer.valueOf(404), statusCaptor.getValue(), "Should return not found status code."); |
||||
|
then(service).should().getSmallPlayer(any()); |
||||
|
then(service).shouldHaveNoMoreInteractions(); |
||||
|
} |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue