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); }
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.
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.
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.
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With