MocktioExtension being too strict with stubs in `@BeforeEach` method
See original GitHub issueI’ve written a test case for a class which persists errors to a file and thus I have a handful of tests to prove that non-errors cases are not persisted to a file however the majority of the tests in the test case are related to triggering various error scenarios.
As the majority of test cases (~70%) require mock to supply the persistence file I’ve included a stub for that in the setup method (annotated using junit5
@BeforeEach
). I’m running the tests using MockitoExtension
which fails tests with org.mockito.exceptions.misusing.UnnecessaryStubbingException
for the 30% of test which demonstrate that the file isn’t use when messaging works.
While it is true that the stub is unused in those test I think it is discouraging readability by duplicating the stubbing to the majority of the tests in the class. From the UnnecessaryStubbingException Javadoc
Mockito JUnit Runner triggers UnnecessaryStubbingException only when none of the test methods use the stubbings. This means that it is ok to put default stubbing in a ‘setup’ method or in test class constructor. That default stubbing needs to be used at least once by one of the test methods.
I expected the Extension to offer the same behaviour.
I’ve contrived a simple example below which fails.
I think this is related to: https://github.com/mockito/mockito/issues/769 but I think it is more of an issues with the MockitoExtension
than strictness it self.
Dependencies on java 8 (8.0.171-oracle):
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.3.1</version>
<scope>test</scope>
</dependency><dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.23.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>2.23.4</version>
<scope>test</scope>
</dependency>
package nz.samandems.mockito;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
@ExtendWith(MockitoExtension.class)
class MockitoLenienceyTestCase {
@Mock
private Dependency dependency;
private SystemUnderTest systemUnderTest;
@BeforeEach
void setUp() {
Mockito.when(dependency.doWork()).thenReturn(true);
systemUnderTest = new SystemUnderTest(dependency);
}
@Test
void shouldDoWork() {
//Given
//When
systemUnderTest.doThing(true);
//Then
Mockito.verify(dependency).doWork();
}
@Test
void shouldDoNoWork() {
//Given
//When
systemUnderTest.doThing(false);
//Then
Mockito.verifyZeroInteractions(dependency);
}
private class SystemUnderTest {
private final Dependency dependency;
private SystemUnderTest(final Dependency dependency) {
this.dependency = dependency;
}
public boolean doThing(boolean works) {
if (works) {
return dependency.doWork();
}
else {
return false;
}
}
}
private interface Dependency {
boolean doWork();
}
}
Issue Analytics
- State:
- Created 5 years ago
- Reactions:12
- Comments:18 (6 by maintainers)
Top GitHub Comments
alternative:
This way, only the specific stubbing is “lenient”.
Any news on this ? Same probleme 90% of my tests need a when call and the other 10% throw exception.
I do not want to do lenient though as i feel UnnecessaryStubbingException is good just need to check with the whole tests