Summary: I am confused about how to handle a single "Map.Entry" when returned from a method call. In my specific case, I need to mock this (using mockito presently) but my question is as much about how to deal with "Map.Entry" as a single unit than it is about mocking it... Help with both would be greatly appreciated.
============================
I have a method like the following. I need to create a matching entity (a Map.Entry I assume) for the mock to return when the method is called. I don't know how to create a single Map.Entry. I've traced calls all the way back to the sql call to the db, but can only find a point where the returned Object is cast to a Map.Entry.
No indication of how to build such a thing. I need help with how to build a single "Map.Entry" that can be returned by the mock.
public Map.Entry<Date,Boolean> getLastModified(SomeClass someClass)
throws Exception
{
return clusterViewDataProvider.getClusterModified(someClass);
}
Here is a line that would create the mocked object for me. For simplicity, assume that the method above is in the "Foo" class.
Foo foo = mock(Foo.class);
Then I would need something like this to say that when the method is called (as a mock) it should return the "Map.Entry" that I need to build.
when(foo.getLastModifiedGid(any())).thenReturn(the Map.Entry I don't know how to make yet);
Lastly, I need to assert something testable about the Map.Entry that is returned. I'm fine to check the key and value in the assert - nothing fancier than that is required - again, I'm not sure how I'd access this as a single Map.Entry...
assertTrue(The Key == SomeDate)
assertTrue(The Value == True)
If I've been unclear on anything, please comment and I will clarify. Gratzi.
There is no need to create your own Map.Entry implementation. You can use one of the existing implementations like AbstractMap.SimpleEntry:
Date date = new Date();
boolean value = true;
Map.Entry<Date, Boolean> entry = new AbstractMap.SimpleEntry<>(date, value);
when(foo.getLastModifiedGid(any())).thenReturn(entry);
or AbstractMap.SimpleImmutableEntry :
Map.Entry<Date, Boolean> entry = new AbstractMap.SimpleImmutableEntry<>(date, value);
As a side note, date classes from java.util are obesolete and classes from java.time should be used for Java 8+. Specifically, java.util.Date is replaced by java.time.Instant.
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