Reputation: 835
I have a Mockito test that uses Argument Captor to verify a new user was POSTed properly:
@Test
public void testPostUser() throws Exception{
User user = new User(1L, "tonkatruck");
when(userService.addUser(any(User.class))).thenReturn(user);
mockMvc.perform(post("/api/user")
.content("{\"userId\":\"1\",\"userName\":\"tonkatruck\"}")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.userName", is(user.getUserName())))
.andExpect(status().isCreated())
.andDo(print());
//verify(userService).addUser(any(User.class));
verify(userService).addUser(userCaptor.capture());
//get the username from what was captured in the POST method and assert that it posted the correct username
String username = userCaptor.getValue().getUserName();
assertEquals("tonkatruck", username);
}
The test fails with an error of:
Failed tests: testPostUser(com.ucrisko.libroomreserve.tests.UserControllerTest): expected:<[tonkatruck]> but was:<[{"userId":"1","userName":"tonkatruck"}]>
I am calling the User class's getUserName() method on the userCaptor object, so any idea why the assertion comes out as the entire JSON User object?
Upvotes: 0
Views: 2101
Reputation: 760
To reproduce your error, I've tried your test. Some assumptions have been done to implement the controller and service. Please find an update of the class test :
package mvc;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import javax.ws.rs.core.MediaType;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
public class UserTest {
@InjectMocks
private UserController userController;
@Mock
private UserService userService;
@Captor
private ArgumentCaptor<User> userCaptor;
private MockMvc mockMvc;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders.standaloneSetup(userController).build();
}
@Test
public void testPostUser() throws Exception {
User user = new User(1L, "tonkatruck");
when(userService.addUser(any(User.class))).thenReturn(user);
mockMvc.perform(post("/api/user") //
.contentType(MediaType.APPLICATION_JSON) //
.content("{\"userId\":\"1\",\"userName\":\"tonkatruck\"}")) //
.andExpect(jsonPath("$.userName", is(user.getUserName()))) //
.andExpect(status().isOk()).andDo(print());
Mockito.verify(userService).addUser(userCaptor.capture());
assertEquals("tonkatruck", userCaptor.getValue().getUserName());
}
}
Maven dependencies used for the test :
<dependencies>
<dependency>
<groupId>com.jayway.restassured</groupId>
<artifactId>spring-mock-mvc</artifactId>
<version>2.6.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.0.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Result : OK
Upvotes: 3