You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
2.2 KiB
70 lines
2.2 KiB
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();
|
|
}
|
|
}
|