-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Closed
Description
Hey,
I'm not sure the title describes the problem in the best way but I found the following.
Consider the two tests:
public class MockitoMisusingUnitTest {
@Test
public void givenNotASpy_whenDoReturn_thenThrowNotAMock() {
try {
List<String> list = new ArrayList<String>();
Mockito.doReturn(100, Mockito.withSettings().lenient())
.when(list)
.size();
fail("Should have thrown a NotAMockException because 'list' is not a mock!");
} catch (NotAMockException e) {
assertThat(e.getMessage(), containsString("Argument passed to when() is not a mock!"));
}
}
}@RunWith(MockitoJUnitRunner.class)
public class MockitoVoidMethodsUnitTest {
@Test
public void whenAddCalledValueCaptured() {
ArrayList<String> mockVoid = mock(ArrayList.class);
ArgumentCaptor<String> valueCapture = ArgumentCaptor.forClass(String.class);
doNothing().when(mockVoid).add(any(Integer.class), valueCapture.capture());
mockVoid.add(0, "captured");
assertEquals("captured", valueCapture.getValue());
}
}When run independently pass. When run together from eclipse or mvn the second test MockitoVoidMethodsUnitTest.whenAddCalledValueCaptured fails but because of something detected in the other test:
org.mockito.exceptions.misusing.UnfinishedStubbingException:
Unfinished stubbing detected here:
-> at org.baeldung.mockito.misusing.MockitoMisusingUnitTest.givenNotASpy_whenDoReturn_thenThrowNotAMock(MockitoMisusingUnitTest.java:28)
E.g. thenReturn() may be missing.
Examples of correct stubbing:
when(mock.isOk()).thenReturn(true);
when(mock.isOk()).thenThrow(exception);
doThrow(exception).when(mock).someVoidMethod();
Hints:
1. missing thenReturn()
2. you are trying to stub a final method, which is not supported
3: you are stubbing the behaviour of another mock inside before 'thenReturn' instruction if completed
-> at org.baeldung.mockito.MockitoVoidMethodsUnitTest.whenAddCalledValueCaptured(MockitoVoidMethodsUnitTest.java:30)
This is the call stack:
MockingProgressImpl.validateState() line: 109
MockingProgressImpl.stubbingStarted() line: 98
MockitoCore.stubber(Strictness) line: 179
MockitoCore.stubber() line: 174
Mockito.doNothing() line: 2421
MockitoVoidMethodsUnitTest.whenAddCalledValueCaptured() line: 30
And the method where it then fails inside MockingProgress.java:
public void validateState() {
validateMostStuff();
//validate stubbing:
if (stubbingInProgress != null) {
Location temp = stubbingInProgress;
stubbingInProgress = null;
throw unfinishedStubbing(temp);
}
}I think this is a bug, tests should be independent and not cause side affects on each other. Maybe something is not cleaned up properly between test runs.
If I add this tear down to MockitoMisusingUnitTest then it works ok:
@After
public void tearDown() {
ThreadSafeMockingProgress.mockingProgress().reset();
} I don't mind to have a go at fixing it, but I would need some help or hints. This was tested with the latest version 2.24.5.
Thanks
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels