java - Mockito : Mocking and returning a different type value -
is possible return different type using when-return in mockito.
my function
m.finddocument(id)
returns document based on id
converting string further processing. but, testing fetching string id file. so, want string returned when function called below :
when(m.finddocument(id)).thenreturn('that_string_from_id_file');
since, 1 of document type , other of string, there way in mockito can same ?
thanks
thing is: using mocking framework doesn't change java language.
when signature of method public foo bar()
- when calling bar()
on mocked object, method has return instance of foo. can't use mocking silently change declared return type of method.
but of course, can do:
document mockeddocument = mock(document.class); documentfinder mockedfinder = mock(documentfinder); when(mockedfinder.finddocument(id)).thenreturn(mockeddocument); when(mockeddocument.getsomeinfo()).thenreturn("that string");
but please note: mock document instance in case can't use "real" document instance. goal should only use mocking impossible/too-hard without dealing mocks.
Comments
Post a Comment