Search in sources :

Example 1 with Strictness

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();
    }
}
Also used : Invocation(org.mockito.invocation.Invocation) Strictness(org.mockito.quality.Strictness) StrictnessSelector.determineStrictness(org.mockito.internal.stubbing.StrictnessSelector.determineStrictness)

Example 2 with Strictness

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);
}
Also used : DefaultMockitoSession(org.mockito.internal.framework.DefaultMockitoSession) MockitoLogger(org.mockito.plugins.MockitoLogger) Strictness(org.mockito.quality.Strictness)

Example 3 with Strictness

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();
    }
}
Also used : Invocation(org.mockito.invocation.Invocation) MatchableInvocation(org.mockito.invocation.MatchableInvocation) SingleRegisteredInvocation(org.mockito.internal.verification.SingleRegisteredInvocation) ValidableAnswer(org.mockito.stubbing.ValidableAnswer) Strictness(org.mockito.quality.Strictness)

Example 4 with Strictness

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);
}
Also used : MockitoSession(org.mockito.MockitoSession) MockitoSessionLoggerAdapter(org.mockito.internal.session.MockitoSessionLoggerAdapter) Strictness(org.mockito.quality.Strictness)

Aggregations

Strictness (org.mockito.quality.Strictness)4 Invocation (org.mockito.invocation.Invocation)2 MockitoSession (org.mockito.MockitoSession)1 DefaultMockitoSession (org.mockito.internal.framework.DefaultMockitoSession)1 MockitoSessionLoggerAdapter (org.mockito.internal.session.MockitoSessionLoggerAdapter)1 StrictnessSelector.determineStrictness (org.mockito.internal.stubbing.StrictnessSelector.determineStrictness)1 SingleRegisteredInvocation (org.mockito.internal.verification.SingleRegisteredInvocation)1 MatchableInvocation (org.mockito.invocation.MatchableInvocation)1 MockitoLogger (org.mockito.plugins.MockitoLogger)1 ValidableAnswer (org.mockito.stubbing.ValidableAnswer)1