Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test Failure Message in mockito : Argument(s) are different! Wanted:

Tags:

I am testing a Restful endpoint in my JUnit and getting an exception as below in the list which is present as an argument inside the save method,

**"Argument(s) are different! Wanted:"**  save( "121", [com.domain.PP@6809cf9d,  com.domain.PP@5925d603] ); Actual invocation has different arguments: save( "121", [com.domain.PP@5b6e23fd,   com.domain.PP@1791fe40]  );  

When I debugged the code, the code broke at the verify line below and threw the above exception. Looks like the arguments inside the "testpPList" within the save method is different. I dont know how it becomes different as I construct them in my JUNit properly and then RestFul URL is invoked.

Requesting your valuable inputs. Thanks.

Code:

@Test public void testSelected() throws Exception {     mockMvc.perform(put("/endpointURL")         .contentType(TestUtil.APPLICATION_JSON_UTF8)         .content(TestUtil.convertObjectToJsonBytes(testObject)))         .andExpect(status().isOk());     verify(programServiceMock, times(1)).save(id, testpPList);     verifyNoMoreInteractions(programServiceMock); } 

Controller method:

@RequestMapping(value = "/endpointURL", method = RequestMethod.PUT) public @ResponseBody void uPP(@PathVariable String id, @RequestBody List<PPView> pPViews) {     // Code to construct the list which is passed into the save method below     save(id, pPList); } 
like image 622
Tom Morris Avatar asked Aug 09 '14 00:08

Tom Morris


People also ask

How verify method works in Mockito?

Mockito. verify() will just verify that an invocation was done on a mock during the method execution. It means that if the real implementation of the mocked class doesn't work as it should, the test is helpless. As a consequence, a class/method that you mock in a test have also to be unitary tested.

What is difference between JUnit and Mockito?

JUnit is the Java library used to write tests (offers support for running tests and different extra helpers - like setup and teardown methods, test sets etc.). Mockito is a library that enables writing tests using the mocking approach.

Does Mockito verify call the method?

Mockito Verify methods are used to check that certain behavior happened. We can use Mockito verify methods at the end of the testing method code to make sure that specified methods are called.


2 Answers

Implementing the Object#equals(Object) can solve it by the equality comparison. Nonetheless, sometimes the object you are validating cannot be changed or its equals function can not be implemented. For such cases, it's recommended using org.mockito.Matchers#refEq(T value, String... excludeFields). So you may use something like:

verify(programServiceMock, times(1)).save(id, refEq(testpPList)); 

Just wrapping the argument with refEq solves the problem.

like image 82
EliuX Avatar answered Sep 19 '22 19:09

EliuX


Make sure you implement the equals method in com.domain.PP.

[Edit]

The reasoning for this conclusion is that your failed test message states that it expects this list of PP

[com.domain.PP@6809cf9d, com.domain.PP@5925d603] 

but it's getting this list of PP

[com.domain.PP@5b6e23fd, com.domain.PP@1791fe40] 

The hex values after the @ symbol for each PP object is their hash codes. Because they are different, then it shows that they belong to different objects. So the default implementation of equals will say they're not equal, which is what verify() uses.

It's good practice to also implement hashCode() whenever you implement equals(): According to the definition of hashCode, two objects that are equal MUST have equal hashCodes. This ensures that objects like HashMap can use hashCode inequality as a shortcut for object inequality (here, placing objects with different hashCodes in different buckets).

like image 24
fgb Avatar answered Sep 20 '22 19:09

fgb