Small extension for Web Controller test, now works using a stub instead of mock

This commit is contained in:
ronsei 2021-11-21 15:42:38 +01:00
parent b027a83aad
commit a923fb1adc

View File

@ -9,6 +9,7 @@ import ch.unisg.tapastasks.tasks.domain.Task;
import org.json.JSONObject; import org.json.JSONObject;
import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
@ -34,7 +35,6 @@ public class AddNewTaskToTaskListWebControllerTest {
@MockBean @MockBean
TaskRepository taskRepository; TaskRepository taskRepository;
@Disabled
@Test @Test
void testAddNewTaskToTaskList() throws Exception { void testAddNewTaskToTaskList() throws Exception {
@ -48,9 +48,17 @@ public class AddNewTaskToTaskListWebControllerTest {
.put("originalTaskUri",originalTaskUri) .put("originalTaskUri",originalTaskUri)
.toString(); .toString();
//This raises a NullPointerException since it tries to build the HTTP response with attributes from Task taskStub = Task.createTaskWithNameAndTypeAndOriginalTaskUri(new Task.TaskName(taskName),
//the domain object (created task), which is mocked --> we need System Tests here! new Task.TaskType(taskType), new Task.OriginalTaskUri(originalTaskUri));
//See the buckpal example from the lecture for a working integration test for testing the web controller
AddNewTaskToTaskListCommand addNewTaskToTaskListCommand = new AddNewTaskToTaskListCommand(
new Task.TaskName(taskName), new Task.TaskType(taskType),
Optional.of(new Task.OriginalTaskUri(originalTaskUri))
);
Mockito.when(addNewTaskToTaskListUseCase.addNewTaskToTaskList(addNewTaskToTaskListCommand))
.thenReturn(taskStub);
mockMvc.perform(post("/tasks/") mockMvc.perform(post("/tasks/")
.contentType(TaskJsonRepresentation.MEDIA_TYPE) .contentType(TaskJsonRepresentation.MEDIA_TYPE)
.content(jsonPayLoad)) .content(jsonPayLoad))