org.mockito.invocation.Invocation Java Examples

The following examples show how to use org.mockito.invocation.Invocation. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: InvocationMarkerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void shouldMarkInvocationsAsVerifiedInOrder() {
    //given
    InOrderContextImpl context = new InOrderContextImpl();
    InvocationMarker marker = new InvocationMarker();
    Invocation i = new InvocationBuilder().toInvocation();
    InvocationMatcher im = new InvocationBuilder().toInvocationMatcher();
    assertFalse(context.isVerified(i));
    assertFalse(i.isVerified());
    
    //when
    marker.markVerifiedInOrder(Arrays.asList(i), im, context);
    
    //then
    assertTrue(context.isVerified(i));
    assertTrue(i.isVerified());
}
 
Example #2
Source File: MockHandlerFactoryTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
//see issue 331
public void valid_handle_result_is_permitted() throws Throwable {
    //given:
    MockCreationSettings settings = (MockCreationSettings) new MockSettingsImpl().defaultAnswer(new Returns(123));
    InternalMockHandler handler = new MockHandlerFactory().create(settings);

    mock.intReturningMethod();
    Invocation invocation = super.getLastInvocation();

    //when:
    Object result = handler.handle(invocation);

    //then
    assertEquals(123, result);
}
 
Example #3
Source File: MockHandlerImplTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void shouldRemoveVerificationModeEvenWhenInvalidMatchers() throws Throwable {
	// given
	Invocation invocation = new InvocationBuilder().toInvocation();
	@SuppressWarnings("rawtypes")
       MockHandlerImpl<?> handler = new MockHandlerImpl(new MockSettingsImpl());
	handler.mockingProgress.verificationStarted(VerificationModeFactory.atLeastOnce());
	handler.matchersBinder = new MatchersBinder() {
		public InvocationMatcher bindMatchers(ArgumentMatcherStorage argumentMatcherStorage, Invocation invocation) {
			throw new InvalidUseOfMatchersException();
		}
	};

	try {
		// when
		handler.handle(invocation);

		// then
		fail();
	} catch (InvalidUseOfMatchersException e) {
	}

	assertNull(handler.mockingProgress.pullVerificationMode());
}
 
Example #4
Source File: ConcurrentCompositeLineConsumerTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
public void verify(VerificationData verificationData) {
  List<Invocation> invocations = verificationData.getAllInvocations();
  InvocationMatcher invocationMatcher = verificationData.getWanted();

  if (invocations == null || invocations.isEmpty()) {
    throw new MockitoException(
        "\nNo interactions with "
            + invocationMatcher.getInvocation().getMock()
            + " mock so far");
  }
  Invocation invocation = invocations.get(invocations.size() - 1);

  if (!invocationMatcher.matches(invocation)) {
    throw new MockitoException("\nWanted but not invoked:\n" + invocationMatcher);
  }
}
 
Example #5
Source File: InvocationContainerImplStubbingTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void should_get_results_for_methods_stub_only() throws Throwable {
    invocationContainerImplStubOnly.setInvocationForPotentialStubbing(new InvocationMatcher(simpleMethod));
    invocationContainerImplStubOnly.addAnswer(new Returns("simpleMethod"));

    Invocation differentMethod = new InvocationBuilder().differentMethod().toInvocation();
    invocationContainerImplStubOnly.setInvocationForPotentialStubbing(new InvocationMatcher(differentMethod));
    invocationContainerImplStubOnly.addAnswer(new ThrowsException(new MyException()));

    assertEquals("simpleMethod", invocationContainerImplStubOnly.answerTo(simpleMethod));

    try {
        invocationContainerImplStubOnly.answerTo(differentMethod);
        fail();
    } catch (MyException e) {}
}
 
Example #6
Source File: InvocationMarker.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void markVerifiedInOrder(List<Invocation> chunk, CapturesArgumensFromInvocation wanted, InOrderContext context) {
    markVerified(chunk, wanted);
    
    for (Invocation i : chunk) {
        context.markVerified(i);
    }
}
 
Example #7
Source File: NoMoreInteractions.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public void verify(VerificationData data) {
    Invocation unverified = new InvocationsFinder().findFirstUnverified(data.getAllInvocations());
    if (unverified != null) {
        new Reporter().noMoreInteractionsWanted(unverified, (List) data.getAllInvocations());
    }
}
 
Example #8
Source File: UnusedStubsFinder.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Finds all unused stubs for given mocks
 * 
 * @param mocks
 */
public List<Invocation> find(List<?> mocks) {
    List<Invocation> unused = new LinkedList<Invocation>();
    for (Object mock : mocks) {
        InternalMockHandler<Object> handler = new MockUtil().getMockHandler(mock);
        List<StubbedInvocationMatcher> fromSingleMock = handler.getInvocationContainer().getStubbedInvocations();
        for(StubbedInvocationMatcher s : fromSingleMock) {
            if (!s.wasUsed()) {
                 unused.add(s.getInvocation());
            }
        }
    }
    return unused;
}
 
Example #9
Source File: InvocationImplTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldEqualToNotConsiderSequenceNumber() {
    Invocation equal = new InvocationBuilder().args(" ").mock("mock").seq(2).toInvocation();
    
    assertTrue(invocation.equals(equal));
    assertTrue(invocation.getSequenceNumber() != equal.getSequenceNumber());
}
 
Example #10
Source File: InvocationsFinderTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldGetLastStackTrace() throws Exception {
    Location last = finder.getLastLocation(invocations);
    assertSame(differentMethodInvocation.getLocation(), last);
    
    assertNull(finder.getLastLocation(Collections.<Invocation>emptyList()));
}
 
Example #11
Source File: InvocationsFinderTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldFindFirstSimilarInvocationByName() throws Exception {
    Invocation overloadedSimpleMethod = new InvocationBuilder().mock(mock).simpleMethod().arg("test").toInvocation();
    
    Invocation found = finder.findSimilarInvocation(invocations, new InvocationMatcher(overloadedSimpleMethod));
    assertSame(found, simpleMethodInvocation);
}
 
Example #12
Source File: InvocationNotifierHandler.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private void notifyMethodCallException(Invocation invocation, Throwable exception) {
	for (InvocationListener listener : invocationListeners) {
           try {
               listener.reportInvocation(new NotifiedMethodInvocationReport(invocation, exception));
           } catch(Throwable listenerThrowable) {
               new Reporter().invocationListenerThrewException(listener, listenerThrowable);
           }
       }
}
 
Example #13
Source File: InvocationMatcherTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void should_capture_varargs_as_vararg() throws Exception {
    //given
    mock.mixedVarargs(1, "a", "b");
    Invocation invocation = getLastInvocation();
    CapturingMatcher m = new CapturingMatcher();
    InvocationMatcher invocationMatcher = new InvocationMatcher(invocation, (List) asList(new Equals(1), new LocalizedMatcher(m)));

    //when
    invocationMatcher.captureArgumentsFrom(invocation);

    //then
    Assertions.assertThat(m.getAllValues()).containsExactly("a", "b");
}
 
Example #14
Source File: ArgumentsExtractorVerifier.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void verify(final VerificationData data) {
    List<Invocation> actualInvocations =
        InvocationsFinder.findInvocations(data.getAllInvocations(), data.getTarget());
    if (actualInvocations.size() != 1) {
        throw new MockitoException("This verifier can only be used with 1 invocation, got "
                + actualInvocations.size());
    }
    Invocation invocation = actualInvocations.get(0);
    arguments = invocation.getArguments();
    invocation.markVerified();
}
 
Example #15
Source File: ArgumentsComparatorTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldNotAllowAnyObjectWithMixedVarargs() {
    //given
    mock.mixedVarargs(1, "1", "2");
    Invocation invocation = getLastInvocation();
    InvocationMatcher invocationMatcher = new InvocationMatcher(invocation, (List) asList(new Equals(1)));

    //when
    boolean match = comparator.argumentsMatch(invocationMatcher, invocation);

    //then
    assertFalse(match);
}
 
Example #16
Source File: ArgumentsComparatorTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldKnowWhenVarargsMatch() {
    //given
    mock.varargs("1", "2", "3");
    Invocation invocation = getLastInvocation();
    InvocationMatcher invocationMatcher = new InvocationMatcher(invocation, (List) asList(new Equals("1"), Any.ANY, new InstanceOf(String.class)));

    //when
    boolean match = comparator.argumentsMatch(invocationMatcher, invocation);

    //then
    assertTrue(match);
}
 
Example #17
Source File: InOrderImplTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldMarkVerifiedInOrder() throws Exception {
    //given
    InOrderImpl impl = new InOrderImpl((List) asList(mock));
    Invocation i = new InvocationBuilder().toInvocation();
    assertFalse(impl.isVerified(i));
    
    //when
    impl.markVerified(i);
    
    //then
    assertTrue(impl.isVerified(i));
}
 
Example #18
Source File: InvocationMatcherTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void should_capture_arguments_from_invocation() throws Exception {
    //given
    Invocation invocation = new InvocationBuilder().args("1", 100).toInvocation();
    CapturingMatcher capturingMatcher = new CapturingMatcher();
    InvocationMatcher invocationMatcher = new InvocationMatcher(invocation, (List) asList(new Equals("1"), capturingMatcher));

    //when
    invocationMatcher.captureArgumentsFrom(invocation);

    //then
    assertEquals(1, capturingMatcher.getAllValues().size());
    assertEquals(100, capturingMatcher.getLastValue());
}
 
Example #19
Source File: ArgumentsComparatorTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldNotAllowAnyObjectMatchEntireVararg() {
    //given
    mock.varargs("1", "2");
    Invocation invocation = getLastInvocation();
    InvocationMatcher invocationMatcher = new InvocationMatcher(invocation, (List) asList(Any.ANY));

    //when
    boolean match = comparator.argumentsMatch(invocationMatcher, invocation);

    //then
    assertFalse(match);
}
 
Example #20
Source File: InvocationsFinder.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private List<Invocation> removeVerifiedInOrder(List<Invocation> invocations, InOrderContext orderingContext) {
    List<Invocation> unverified = new LinkedList<Invocation>();
    for (Invocation i : invocations) {
        if (orderingContext.isVerified(i)) {
            unverified.clear();
        } else {
            unverified.add(i);
        }
    }
    return unverified;
}
 
Example #21
Source File: NumberOfInvocationsInOrderChecker.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void check(List<Invocation> invocations, InvocationMatcher wanted, int wantedCount, InOrderContext context) {
    List<Invocation> chunk = finder.findMatchingChunk(invocations, wanted, wantedCount, context);
    
    int actualCount = chunk.size();
    
    if (wantedCount > actualCount) {
        Location lastInvocation = finder.getLastLocation(chunk);
        reporter.tooLittleActualInvocationsInOrder(new Discrepancy(wantedCount, actualCount), wanted, lastInvocation);
    } else if (wantedCount < actualCount) {
        Location firstUndesired = chunk.get(wantedCount).getLocation();
        reporter.tooManyActualInvocationsInOrder(wantedCount, actualCount, wanted, firstUndesired);
    }
    
    invocationMarker.markVerifiedInOrder(chunk, wanted, context);
}
 
Example #22
Source File: ArgumentsComparatorTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldNotMatchWhenSomeOtherArgumentDoesNotMatch() {
    //given
    mock.mixedVarargs(1, "1", "2");
    Invocation invocation = getLastInvocation();
    InvocationMatcher invocationMatcher = new InvocationMatcher(invocation, (List) asList(new Equals(100), AnyVararg.ANY_VARARG));

    //when
    boolean match = comparator.argumentsMatch(invocationMatcher, invocation);

    //then
    assertFalse(match);
}
 
Example #23
Source File: OnlyTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldNotMarkAsVerifiedWhenAssertionFailed() {
    //given
    Invocation invocation = new InvocationBuilder().toInvocation();
    assertFalse(invocation.isVerified());
    
    //when
    try {
        only.verify(new VerificationDataStub(new InvocationBuilder().toInvocationMatcher(), invocation));
        fail();
    } catch (MockitoAssertionError e) {}
    
    //then
    assertFalse(invocation.isVerified());
}
 
Example #24
Source File: AllInvocationsFinderTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldNotCountDuplicatedInteractions() throws Exception {
    mockOne.simpleMethod(100);

    List<Invocation> invocations = finder.find(asList(mockOne, mockOne, mockOne));

    assertEquals(1, invocations.size());
}
 
Example #25
Source File: MockitoUtils.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private static Object[] getInvocationArguments(Invocation invocation, InvocationArgumentsAdapter... argumentAdapters) {
	Object[] arguments = invocation.getArguments();
	for (InvocationArgumentsAdapter adapter : argumentAdapters) {
		arguments = adapter.adaptArguments(arguments);
	}
	return arguments;
}
 
Example #26
Source File: NumberOfInvocationsInOrderCheckerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Before
public void setup() {
    reporter = new Reporter();
    finderStub = new InvocationsFinderStub();
    checker = new NumberOfInvocationsInOrderChecker(finderStub, reporter);
    
    wanted = new InvocationBuilder().toInvocationMatcher();
    invocations = new LinkedList<Invocation>(asList(new InvocationBuilder().toInvocation()));
}
 
Example #27
Source File: InvocationsFinderTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldFindActualInvocations() throws Exception {
    List<Invocation> actual = finder.findInvocations(invocations, new InvocationMatcher(simpleMethodInvocation));
    assertThat(actual, hasExactlyInOrder(simpleMethodInvocation, simpleMethodInvocationTwo));
    
    actual = finder.findInvocations(invocations, new InvocationMatcher(differentMethodInvocation));
    assertThat(actual, hasExactlyInOrder(differentMethodInvocation));
}
 
Example #28
Source File: ArgumentsComparatorTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldAllowAnyVarargMatchEntireVararg() {
    //given
    mock.varargs("1", "2");
    Invocation invocation = getLastInvocation();
    InvocationMatcher invocationMatcher = new InvocationMatcher(invocation, (List) asList(AnyVararg.ANY_VARARG));

    //when
    boolean match = comparator.argumentsMatch(invocationMatcher, invocation);

    //then
    assertTrue(match);
}
 
Example #29
Source File: InvocationImplTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldScreamWhenCallingRealMethodOnInterface() throws Throwable {
    //given
    Invocation invocationOnInterface = new InvocationBuilder().toInvocation();

    try {
        //when
        invocationOnInterface.callRealMethod();
        //then
        fail();
    } catch(MockitoException e) {}
}
 
Example #30
Source File: NoMoreInteractionsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldVerifyInOrderMultipleInvoctions() {
    //given
    NoMoreInteractions n = new NoMoreInteractions();
    Invocation i = new InvocationBuilder().seq(1).toInvocation();
    Invocation i2 = new InvocationBuilder().seq(2).toInvocation();

    //when
    context.markVerified(i2);
    
    //then no exception is thrown
    n.verifyInOrder(new VerificationDataInOrderImpl(context, asList(i, i2), null));
}