From a923fb1adc8d7ffc57792de0ed3dc88f760bf5e1 Mon Sep 17 00:00:00 2001 From: ronsei Date: Sun, 21 Nov 2021 15:42:38 +0100 Subject: [PATCH] Small extension for Web Controller test, now works using a stub instead of mock --- .../AddNewTaskToTaskListWebControllerTest.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/tapas-tasks/src/test/java/ch/unisg/tapastasks/tasks/adapter/in/web/AddNewTaskToTaskListWebControllerTest.java b/tapas-tasks/src/test/java/ch/unisg/tapastasks/tasks/adapter/in/web/AddNewTaskToTaskListWebControllerTest.java index d397908..c940a9d 100644 --- a/tapas-tasks/src/test/java/ch/unisg/tapastasks/tasks/adapter/in/web/AddNewTaskToTaskListWebControllerTest.java +++ b/tapas-tasks/src/test/java/ch/unisg/tapastasks/tasks/adapter/in/web/AddNewTaskToTaskListWebControllerTest.java @@ -9,6 +9,7 @@ import ch.unisg.tapastasks.tasks.domain.Task; import org.json.JSONObject; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; +import org.mockito.Mockito; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; @@ -34,7 +35,6 @@ public class AddNewTaskToTaskListWebControllerTest { @MockBean TaskRepository taskRepository; - @Disabled @Test void testAddNewTaskToTaskList() throws Exception { @@ -48,9 +48,17 @@ public class AddNewTaskToTaskListWebControllerTest { .put("originalTaskUri",originalTaskUri) .toString(); - //This raises a NullPointerException since it tries to build the HTTP response with attributes from - //the domain object (created task), which is mocked --> we need System Tests here! - //See the buckpal example from the lecture for a working integration test for testing the web controller + Task taskStub = Task.createTaskWithNameAndTypeAndOriginalTaskUri(new Task.TaskName(taskName), + new Task.TaskType(taskType), new Task.OriginalTaskUri(originalTaskUri)); + + 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/") .contentType(TaskJsonRepresentation.MEDIA_TYPE) .content(jsonPayLoad))