use of org.mockito.quality.Strictness in project mockito by mockito.
the class DefaultStubbingLookupListener method onStubbingLookup.
@Override
public void onStubbingLookup(StubbingLookupEvent event) {
Strictness actualStrictness = determineStrictness(event.getStubbingFound(), event.getMockSettings(), currentStrictness);
if (actualStrictness != Strictness.STRICT_STUBS) {
return;
}
if (event.getStubbingFound() == null) {
// If stubbing was not found for invocation it means that either the mock invocation was
// not stubbed or
// we have a stubbing arg mismatch.
List<Invocation> argMismatchStubbings = potentialArgMismatches(event.getInvocation(), event.getAllStubbings());
if (!argMismatchStubbings.isEmpty()) {
mismatchesReported = true;
Reporter.potentialStubbingProblem(event.getInvocation(), argMismatchStubbings);
}
} else {
// when strict stubs are in use, every time a stub is realized in the code it is
// implicitly marked as verified
// this way, the users don't have to repeat themselves to verify stubbed invocations
// (DRY)
event.getInvocation().markVerified();
}
}
use of org.mockito.quality.Strictness in project mockito by mockito.
the class DefaultMockitoSessionBuilder method startMocking.
@Override
public MockitoSession startMocking() {
// Configure default values
List<Object> effectiveTestClassInstances;
String effectiveName;
if (testClassInstances.isEmpty()) {
effectiveTestClassInstances = emptyList();
effectiveName = this.name == null ? "<Unnamed Session>" : this.name;
} else {
effectiveTestClassInstances = new ArrayList<>(testClassInstances);
Object lastTestClassInstance = testClassInstances.get(testClassInstances.size() - 1);
effectiveName = this.name == null ? lastTestClassInstance.getClass().getName() : this.name;
}
Strictness effectiveStrictness = this.strictness == null ? Strictness.STRICT_STUBS : this.strictness;
MockitoLogger logger = this.logger == null ? Plugins.getMockitoLogger() : new MockitoLoggerAdapter(this.logger);
return new DefaultMockitoSession(effectiveTestClassInstances, effectiveName, effectiveStrictness, logger);
}
use of org.mockito.quality.Strictness in project mockito by mockito.
the class InvocationContainerImpl method addAnswer.
/**
* Adds new stubbed answer and returns the invocation matcher the answer was added to.
*/
public StubbedInvocationMatcher addAnswer(Answer answer, boolean isConsecutive, Strictness stubbingStrictness) {
Invocation invocation = invocationForStubbing.getInvocation();
mockingProgress().stubbingCompleted();
if (answer instanceof ValidableAnswer) {
((ValidableAnswer) answer).validateFor(invocation);
}
synchronized (stubbed) {
if (isConsecutive) {
stubbed.getFirst().addAnswer(answer);
} else {
Strictness effectiveStrictness = stubbingStrictness != null ? stubbingStrictness : this.mockStrictness;
stubbed.addFirst(new StubbedInvocationMatcher(answer, invocationForStubbing, effectiveStrictness));
}
return stubbed.getFirst();
}
}
use of org.mockito.quality.Strictness in project mockito by mockito.
the class MockitoExtension method beforeEach.
/**
* Callback that is invoked <em>before</em> each test is invoked.
*
* @param context the current extension context; never {@code null}
*/
@Override
public void beforeEach(final ExtensionContext context) {
List<Object> testInstances = context.getRequiredTestInstances().getAllInstances();
Strictness actualStrictness = this.retrieveAnnotationFromTestClasses(context).map(MockitoSettings::strictness).orElse(strictness);
MockitoSession session = Mockito.mockitoSession().initMocks(testInstances.toArray()).strictness(actualStrictness).logger(new MockitoSessionLoggerAdapter(Plugins.getMockitoLogger())).startMocking();
context.getStore(MOCKITO).put(MOCKS, new HashSet<>());
context.getStore(MOCKITO).put(SESSION, session);
}
Aggregations