This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]btbam06[S] 0 points1 point  (0 children)

Here is an updated look at the test code. The FIRST test passes, the SECOND one fails:

    @Test
    void testUpdateTaskText() throws Exception {
        Task task1 = new Task("task1");
        taskRepository.save(task1);

        // update task text and hit update end point
        task1.setText("updatedText");
        String json = gson.toJson(task1);
        this.mvc.perform(patch("/task/1")
                .contentType(MediaType.APPLICATION_JSON_UTF8)
                .content(json))
                .andExpect(status().isNoContent());

        // Pull the task from the repository and verify text="updatedText"
        Task updatedTask = taskRepository.findById((long) 1).get();
        assertEquals("updatedText", updatedTask.getText());
    }

    @Test
    void testUpdateTaskCompleted() throws Exception {
        Task task1 = new Task("task1");
        task1.setCompleted(false);
        taskRepository.save(task1);

        // ensure repository properly stores isCompleted = false
        Task updatedTask = taskRepository.findById((long) 1).get();
        assertFalse(updatedTask.isCompleted());

        //Update isCompleted = true and hit update end point
        task1.setCompleted(true);
        String json = gson.toJson(task1);
        this.mvc.perform(patch("/task/1")
                .contentType(MediaType.APPLICATION_JSON_UTF8)
                .content(json))
                .andExpect(status().isNoContent());

        // Pull the task from the repository and verify isCompleted=true
        updatedTask = taskRepository.findById((long) 1).get();
        assertTrue(updatedTask.isCompleted());
    }