Search in sources :

Example 46 with Invocation

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

the class OnlyTest method shouldMarkAsVerified.

@Test
public void shouldMarkAsVerified() {
    //given
    Invocation invocation = new InvocationBuilder().toInvocation();
    assertFalse(invocation.isVerified());
    //when
    only.verify(new VerificationDataStub(new InvocationMatcher(invocation), invocation));
    //then
    assertTrue(invocation.isVerified());
}
Also used : Invocation(org.mockito.invocation.Invocation) MatchableInvocation(org.mockito.invocation.MatchableInvocation) InvocationMatcher(org.mockito.internal.invocation.InvocationMatcher) InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) Test(org.junit.Test)

Example 47 with Invocation

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

the class InvocationsFinderTest method shouldFindInvocationWithTheSameMethod.

@Test
public void shouldFindInvocationWithTheSameMethod() throws Exception {
    Invocation overloadedDifferentMethod = new InvocationBuilder().differentMethod().arg("test").toInvocation();
    invocations.add(overloadedDifferentMethod);
    Invocation found = InvocationsFinder.findSimilarInvocation(invocations, new InvocationMatcher(overloadedDifferentMethod));
    assertSame(found, overloadedDifferentMethod);
}
Also used : Invocation(org.mockito.invocation.Invocation) Test(org.junit.Test)

Example 48 with Invocation

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

the class MapOperationProviderDelegatorTest method testDelegator.

@Test
public void testDelegator() throws Exception {
    Method[] methods = MapOperationProviderDelegator.class.getDeclaredMethods();
    // invoke all methods of MapOperationProviderDelegator
    for (Method method : methods) {
        if (isAbstract(method.getModifiers()) || isPrivate(method.getModifiers())) {
            continue;
        }
        Object[] parameters = getParameters(method);
        try {
            method.invoke(delegator, parameters);
        } catch (Exception e) {
            System.err.println(format("Could not invoke method %s: %s", method.getName(), e.getMessage()));
        }
    }
    // get a list of all method invocations from Mockito
    List<String> methodsCalled = new ArrayList<String>();
    MockingDetails mockingDetails = Mockito.mockingDetails(operationProvider);
    Collection<Invocation> invocations = mockingDetails.getInvocations();
    for (Invocation invocation : invocations) {
        methodsCalled.add(invocation.getMethod().getName());
    }
    // verify that all methods have been called on the delegated MapOperationProvider
    for (Method method : methods) {
        if (isAbstract(method.getModifiers()) || isPrivate(method.getModifiers())) {
            continue;
        }
        assertTrue(format("Method %s() should have been called", method.getName()), methodsCalled.contains(method.getName()));
    }
}
Also used : Invocation(org.mockito.invocation.Invocation) ArrayList(java.util.ArrayList) MockingDetails(org.mockito.MockingDetails) Method(java.lang.reflect.Method) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 49 with Invocation

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

the class WarningsFinder method find.

public void find(FindingsListener findingsListener) {
    List<Invocation> unusedStubs = new LinkedList<Invocation>(this.baseUnusedStubs);
    List<InvocationMatcher> allInvocations = new LinkedList<InvocationMatcher>(this.baseAllInvocations);
    Iterator<Invocation> unusedIterator = unusedStubs.iterator();
    while (unusedIterator.hasNext()) {
        Invocation unused = unusedIterator.next();
        Iterator<InvocationMatcher> unstubbedIterator = allInvocations.iterator();
        while (unstubbedIterator.hasNext()) {
            InvocationMatcher unstubbed = unstubbedIterator.next();
            if (unstubbed.hasSimilarMethod(unused)) {
                findingsListener.foundStubCalledWithDifferentArgs(unused, unstubbed);
                unusedIterator.remove();
                unstubbedIterator.remove();
            }
        }
    }
    for (Invocation i : unusedStubs) {
        findingsListener.foundUnusedStub(i);
    }
    for (InvocationMatcher i : allInvocations) {
        findingsListener.foundUnstubbed(i);
    }
}
Also used : Invocation(org.mockito.invocation.Invocation) InvocationMatcher(org.mockito.internal.invocation.InvocationMatcher) LinkedList(java.util.LinkedList)

Example 50 with Invocation

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

the class Reporter method formatUnncessaryStubbingException.

public static UnnecessaryStubbingException formatUnncessaryStubbingException(Class<?> testClass, Collection<Invocation> unnecessaryStubbings) {
    StringBuilder stubbings = new StringBuilder();
    int count = 1;
    for (Invocation u : unnecessaryStubbings) {
        stubbings.append("\n  ").append(count++).append(". ").append(u.getLocation());
    }
    String heading = (testClass != null) ? "Unnecessary stubbings detected in test class: " + testClass.getSimpleName() : "Unnecessary stubbings detected.";
    return new UnnecessaryStubbingException(join(heading, "Clean & maintainable test code requires zero unnecessary code.", "Following stubbings are unnecessary (click to navigate to relevant line of code):" + stubbings, "Please remove unnecessary stubbings or use 'silent' option. More info: javadoc for UnnecessaryStubbingException class."));
}
Also used : Invocation(org.mockito.invocation.Invocation) DescribedInvocation(org.mockito.invocation.DescribedInvocation)

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