Unit testing

The code to unit test the created Todo is shown as follows:

    @Test    public void createTodo() throws Exception {     Todo mockTodo = new Todo(CREATED_TODO_ID, "Jack",      "Learn Spring MVC", new Date(), false);     String todo = "{"user":"Jack","desc":"Learn Spring MVC",          "done":false}";    when(service.addTodo(anyString(), anyString(),       isNull(),anyBoolean()))    .thenReturn(mockTodo);   mvc    .perform(MockMvcRequestBuilders.post("/users/Jack/todos")    .content(todo)    .contentType(MediaType.APPLICATION_JSON)    )    .andExpect(status().isCreated())    .andExpect(      header().string("location",containsString("/users/Jack/todos/"     + CREATED_TODO_ID)));   }

A few important things to note are as follows:

  • String todo = "{"user":"Jack","desc":"Learn Spring MVC","done":false}" ...

Get Mastering Spring 5.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.