-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
2019/02/04/mockito-doreturn-vs-thenreturn #16
Comments
I spy an instance from applicationContext, doreturn when(instance).someVoidMethod(),but I find someVoidMethod had been called; |
@asheCarry First of all, I think it better to call 'spy' on the object you want to monitor directly. You called 'spy' on If you spy on // Mock Service
service = Mockito.mock(ITradeBasicService.class);
Mockito.doThrow(OvsBusinessCheckedException.class).doNothing().when(service).updateStatusAndVersionCompareStatusAndVersionById(Mockito.anyLong(), Mockito.anyString(), Mockito.anyString(), Mockito.anyByte());
// Spy
context = Mockito.spy(applicationContext);
// Spy returns Mock Service
Mockito.doReturn(service).when (context).getBean(ITradeBasicService.class); |
First,thanks for your answer. |
@asheCarry doReturn("foo").when(spy).get(0); Technically, this doesn't call the
|
@SangsooNam
` |
@SangsooNam |
@asheCarry @Test
public void doReturnWithSpyList() {
List list = new LinkedList<String>();
List spy = Mockito.spy(list);
// This doesn't throw IndexOutOfBoundsException
Mockito.doReturn("foo").when(spy).get(0);
Assert.assertEquals("foo", spy.get(0));
} And, when I run your test, it doesn't work and fails with |
@SangsooNam |
Mockito: doReturn vs thenReturn
In Mockito, you can specify what to return when a method is called. That makes unit testing easier because you don’t have to change existing classes. Mockito...
http://sangsoonam.github.io/2019/02/04/mockito-doreturn-vs-thenreturn.html
The text was updated successfully, but these errors were encountered: