Package org.mockito.exceptions.misusing
Class UnnecessaryStubbingException
- java.lang.Object
-
- java.lang.Throwable
-
- java.lang.Exception
-
- java.lang.RuntimeException
-
- org.mockito.exceptions.base.MockitoException
-
- org.mockito.exceptions.misusing.UnnecessaryStubbingException
-
- All Implemented Interfaces:
Serializable
public class UnnecessaryStubbingException extends MockitoException
This exception indicates presence of unused stubbings. It is highly recommended to remove unused stubbings to keep the codebase clean. In a rare scenario that unused stubbing is a false negative you can opt out from the validation via (in order of ascending scope):- Declaring specific stubbing as 'lenient' -
Mockito.lenient()
- Declaring specific mock as 'lenient' -
MockSettings.lenient()
- Declaring all mocks in given test class or test method mock as 'lenient' with
our JUnit support (
MockitoJUnit
) or Mockito session (MockitoSession
)
Unnecessary stubbings are stubbed method calls that were never realized during test execution. Example:
Notice that one of the stubbed methods were never realized in the code under test, during test execution. The stray stubbing might be an oversight of the developer, the artifact of copy-paste or the effect of not understanding the test/code. Either way, the developer ends up with unnecessary test code. In order to keep the codebase clean and maintainable it is necessary to remove unnecessary code. Otherwise tests are harder to read and reason about.//code under test: ... String result = translator.translate("one") ... //test: ... when(translator.translate("one")).thenReturn("jeden"); // <- stubbing realized during code execution when(translator.translate("two")).thenReturn("dwa"); // <- stubbing never realized ...
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.- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description UnnecessaryStubbingException(String message)
-
Method Summary
-
Methods inherited from class org.mockito.exceptions.base.MockitoException
getUnfilteredStackTrace
-
Methods inherited from class java.lang.Throwable
addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString
-
-
-
-
Constructor Detail
-
UnnecessaryStubbingException
public UnnecessaryStubbingException(String message)
-
-