I am writing unit tests in java using mockito.
This is the statement that I am trying to test.
final Map<EntityKey, Element<Movie>> resultMap = Watcher.watch(movies);
movies is Set of movie names which is a key to identify a movie.
I mocked watcher class
final Watcher<Movie> watcher = mock(Watcher.class);
Mockito.when(watcher.watch(Matchers.any(Set.class))).thenReturn()
what to include in “thenReturn”??
In the
thenReturnfunction you need to pass an object of the same type as the method you are mocking’s return type.When this method is then called on that object, it will return the object you passed to
thenReturninstead of actually going into the function.This is the core concept behind mocking.
Having said that. If you are trying to test the Watcher.watch method then you probably don’t want to mock it anyway. You should only mock those classes you are NOT testing.