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}" ...