Search in sources :

Example 11 with Invocation

use of org.mockito.invocation.Invocation in project mockito by mockito.

the class WarningsCollector method getWarnings.

public String getWarnings() {
    List<Invocation> unused = new UnusedStubsFinder().find(createdMocks);
    List<Invocation> all = AllInvocationsFinder.find(createdMocks);
    List<InvocationMatcher> allInvocationMatchers = InvocationMatcher.createFrom(all);
    return new WarningsPrinterImpl(unused, allInvocationMatchers, false).print();
}
Also used : UnusedStubsFinder(org.mockito.internal.invocation.UnusedStubsFinder) Invocation(org.mockito.invocation.Invocation) InvocationMatcher(org.mockito.internal.invocation.InvocationMatcher)

Example 12 with Invocation

use of org.mockito.invocation.Invocation in project mockito by mockito.

the class Reporter method potentialStubbingProblem.

public static void potentialStubbingProblem(Invocation actualInvocation, Collection<Invocation> argMismatchStubbings) {
    StringBuilder stubbings = new StringBuilder();
    int count = 1;
    for (Invocation s : argMismatchStubbings) {
        stubbings.append("    ").append(count++).append(". ").append(s);
        stubbings.append("\n      ").append(s.getLocation()).append("\n");
    }
    //remove trailing end of line
    stubbings.deleteCharAt(stubbings.length() - 1);
    throw new PotentialStubbingProblem(join("Strict stubbing argument mismatch. Please check:", " - this invocation of '" + actualInvocation.getMethod().getName() + "' method:", "    " + actualInvocation, "    " + actualInvocation.getLocation(), " - has following stubbing(s) with different arguments:", stubbings, "Typically, stubbing argument mismatch indicates user mistake when writing tests.", "Mockito fails early so that you can debug potential problem easily.", "However, there are legit scenarios when this exception generates false negative signal:", "  - stubbing the same method multiple times using 'given().will()' or 'when().then()' API", "    Please use 'will().given()' or 'doReturn().when()' API for stubbing.", "  - stubbed method is intentionally invoked with different arguments by code under test", "    Please use 'default' or 'silent' JUnit Rule.", "For more information see javadoc for PotentialStubbingProblem class."));
}
Also used : Invocation(org.mockito.invocation.Invocation) DescribedInvocation(org.mockito.invocation.DescribedInvocation)

Example 13 with Invocation

use of org.mockito.invocation.Invocation in project mockito by mockito.

the class MockitoCore method getLastInvocation.

/**
     * For testing purposes only. Is not the part of main API.
     *
     * @return last invocation
     */
public Invocation getLastInvocation() {
    OngoingStubbingImpl ongoingStubbing = ((OngoingStubbingImpl) mockingProgress().pullOngoingStubbing());
    List<Invocation> allInvocations = ongoingStubbing.getRegisteredInvocations();
    return allInvocations.get(allInvocations.size() - 1);
}
Also used : Invocation(org.mockito.invocation.Invocation) Reporter.missingMethodInvocation(org.mockito.internal.exceptions.Reporter.missingMethodInvocation) OngoingStubbingImpl(org.mockito.internal.stubbing.OngoingStubbingImpl)

Example 14 with Invocation

use of org.mockito.invocation.Invocation in project mockito by mockito.

the class MockHandlerFactoryTest method valid_handle_result_is_permitted.

@Test
public //see issue 331
void valid_handle_result_is_permitted() throws Throwable {
    //given:
    MockCreationSettings<?> settings = (MockCreationSettings<?>) new MockSettingsImpl().defaultAnswer(new Returns(123));
    InternalMockHandler<?> handler = createMockHandler(settings);
    mock.intReturningMethod();
    Invocation invocation = super.getLastInvocation();
    //when:
    Object result = handler.handle(invocation);
    //then
    assertEquals(123, result);
}
Also used : Returns(org.mockito.internal.stubbing.answers.Returns) Invocation(org.mockito.invocation.Invocation) MockCreationSettings(org.mockito.mock.MockCreationSettings) MockSettingsImpl(org.mockito.internal.creation.MockSettingsImpl) Test(org.junit.Test)

Example 15 with Invocation

use of org.mockito.invocation.Invocation in project mockito by mockito.

the class MockHandlerFactoryTest method handle_result_must_not_be_null_for_primitives.

@Test
public //see issue 331
void handle_result_must_not_be_null_for_primitives() throws Throwable {
    //given:
    MockCreationSettings<?> settings = (MockCreationSettings<?>) new MockSettingsImpl().defaultAnswer(new Returns(null));
    InternalMockHandler<?> handler = createMockHandler(settings);
    mock.intReturningMethod();
    Invocation invocation = super.getLastInvocation();
    //when:
    Object result = handler.handle(invocation);
    //then null value is not a valid result for a primitive
    assertNotNull(result);
    assertEquals(0, result);
}
Also used : Returns(org.mockito.internal.stubbing.answers.Returns) Invocation(org.mockito.invocation.Invocation) MockCreationSettings(org.mockito.mock.MockCreationSettings) MockSettingsImpl(org.mockito.internal.creation.MockSettingsImpl) Test(org.junit.Test)

Aggregations

Invocation (org.mockito.invocation.Invocation)106 Test (org.junit.Test)71 InvocationBuilder (org.mockito.internal.invocation.InvocationBuilder)28 MatchableInvocation (org.mockito.invocation.MatchableInvocation)19 InvocationMatcher (org.mockito.internal.invocation.InvocationMatcher)11 Location (org.mockito.invocation.Location)7 InvocationsFinder.getLastLocation (org.mockito.internal.invocation.InvocationsFinder.getLastLocation)6 Returns (org.mockito.internal.stubbing.answers.Returns)6 VerificationDataInOrderImpl (org.mockito.internal.verification.api.VerificationDataInOrderImpl)6 Method (java.lang.reflect.Method)5 Discrepancy (org.mockito.internal.reporting.Discrepancy)4 MissingInvocationChecker.checkMissingInvocation (org.mockito.internal.verification.checkers.MissingInvocationChecker.checkMissingInvocation)4 VerificationInOrderFailure (org.mockito.exceptions.verification.VerificationInOrderFailure)3 InvocationsFinder.findFirstMatchingUnverifiedInvocation (org.mockito.internal.invocation.InvocationsFinder.findFirstMatchingUnverifiedInvocation)3 InOrderContextImpl (org.mockito.internal.verification.InOrderContextImpl)3 IMethods (org.mockitousage.IMethods)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 LinkedList (java.util.LinkedList)2 TestCase.assertEquals (junit.framework.TestCase.assertEquals)2 MockingDetails (org.mockito.MockingDetails)2