I have an interface with the following method
public interface IRemoteStore {
<T> Optional<T> get(String cacheName, String key, String ... rest);
}
The instance of the class implementing the interface is called remoteStore.
When I mock this with mockito and use the method when:
Mockito.when(remoteStore.get("a", "b")).thenReturn("lol");
I get the error:
Cannot resolved the method 'thenReturn(java.lang.String)'
I thought it has to do with the fact that get returns an instance of the Optional class so I tried this:
Mockito.<Optional<String>>when(remoteStore.get("cache-name", "cache-key")).thenReturn
(Optional.of("lol"));
But, I get this error instead:
when (Optional '<'String'>') in Mockito cannot be applied to (Optional'<'Object'>').
The only time it worked was with this:
String returnCacheValueString = "lol";
Optional<Object> returnCacheValue = Optional.of((Object) returnCacheValueString);
Mockito.<Optional<Object>>when(remotestore.get("cache-name", "cache-key")).thenReturn(returnCacheValue);
But above returns an instance of Optional '<'Object'>' and not Optional '<'String'>.
Why couldn't I just return an instance of Optional '<'String'>' directly? If I could, how should I go about doing that?
question from:
https://stackoverflow.com/questions/30946167/mockito-error-with-method-that-returns-optionalt 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…