Search in sources :

Example 51 with InvocationBuilder

use of org.mockito.internal.invocation.InvocationBuilder in project mockito by mockito.

the class ReturnsArgumentAtTest method should_identify_bad_parameter_type_for_invocation.

@Test
public void should_identify_bad_parameter_type_for_invocation() throws Exception {
    try {
        new ReturnsArgumentAt(1).validateFor(new InvocationBuilder().method("varargsReturningString").argTypes(Object[].class).args(new Object(), new Object(), new Object()).toInvocation());
        Assertions.fail("should scream");
    } catch (WrongTypeOfReturnValue ignored) {
    }
    try {
        new ReturnsArgumentAt(0).validateFor(new InvocationBuilder().method("oneArray").argTypes(boolean[].class).args(true, false, false).toInvocation());
        Assertions.fail("should scream");
    } catch (WrongTypeOfReturnValue ignored) {
    }
    try {
        new ReturnsArgumentAt(0).validateFor(new InvocationBuilder().method("mixedVarargsReturningString").argTypes(Object.class, String[].class).args(new Object(), new String[] { "A", "B", "C" }).toInvocation());
        Assertions.fail("should scream");
    } catch (WrongTypeOfReturnValue ignored) {
    }
}
Also used : WrongTypeOfReturnValue(org.mockito.exceptions.misusing.WrongTypeOfReturnValue) InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) Test(org.junit.Test)

Example 52 with InvocationBuilder

use of org.mockito.internal.invocation.InvocationBuilder in project mockito by mockito.

the class ReturnsArgumentAtTest method should_fail_if_index_is_not_in_range_for_example_with_no_arg_invocation.

@Test
public void should_fail_if_index_is_not_in_range_for_example_with_no_arg_invocation() throws Throwable {
    try {
        new ReturnsArgumentAt(ReturnsArgumentAt.LAST_ARGUMENT).validateFor(new InvocationBuilder().simpleMethod().toInvocation());
        fail();
    } catch (MockitoException e) {
        assertThat(e.getMessage()).containsIgnoringCase("invalid argument index").containsIgnoringCase("iMethods.simpleMethod").containsIgnoringCase("no arguments").containsIgnoringCase("last parameter wanted");
    }
}
Also used : MockitoException(org.mockito.exceptions.base.MockitoException) InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) Test(org.junit.Test)

Example 53 with InvocationBuilder

use of org.mockito.internal.invocation.InvocationBuilder in project mockito by mockito.

the class ReturnsTest method should_allow_correct_type_of_return_value.

@Test
public void should_allow_correct_type_of_return_value() {
    new Returns("one").validateFor(new InvocationBuilder().simpleMethod().toInvocation());
    new Returns(false).validateFor(new InvocationBuilder().method("booleanReturningMethod").toInvocation());
    new Returns(TRUE).validateFor(new InvocationBuilder().method("booleanObjectReturningMethod").toInvocation());
    new Returns(1).validateFor(new InvocationBuilder().method("integerReturningMethod").toInvocation());
    new Returns(1L).validateFor(new InvocationBuilder().method("longReturningMethod").toInvocation());
    new Returns(1L).validateFor(new InvocationBuilder().method("longObjectReturningMethod").toInvocation());
    new Returns(null).validateFor(new InvocationBuilder().method("objectReturningMethodNoArgs").toInvocation());
    new Returns(1).validateFor(new InvocationBuilder().method("objectReturningMethodNoArgs").toInvocation());
}
Also used : InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) Test(org.junit.Test)

Example 54 with InvocationBuilder

use of org.mockito.internal.invocation.InvocationBuilder in project mockito by mockito.

the class AnswersWithDelayTest method should_delay.

@Test
public void should_delay() throws Throwable {
    final long sleepyTime = 500L;
    final AnswersWithDelay testSubject = new AnswersWithDelay(sleepyTime, new Returns("value"));
    final Date before = new Date();
    testSubject.answer(new InvocationBuilder().method("oneArg").arg("A").toInvocation());
    final Date after = new Date();
    final long timePassed = after.getTime() - before.getTime();
    assertThat(timePassed).isCloseTo(sleepyTime, within(15L));
}
Also used : InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) Date(java.util.Date) Test(org.junit.Test)

Example 55 with InvocationBuilder

use of org.mockito.internal.invocation.InvocationBuilder in project mockito by mockito.

the class StubbingArgMismatchesTest method multiple_matching_invocations_per_stub_plus_some_other_invocation.

@Test
public void multiple_matching_invocations_per_stub_plus_some_other_invocation() throws Exception {
    // given
    Invocation stubbing = new InvocationBuilder().args("a").location("-> at A.java").toInvocation();
    mismatches.add(new InvocationBuilder().args("x").location("-> at X.java").toInvocation(), stubbing);
    mismatches.add(new InvocationBuilder().args("y").location("-> at Y.java").toInvocation(), stubbing);
    mismatches.add(new InvocationBuilder().method("differentMethod").args("n").location("-> at N.java").toInvocation(), new InvocationBuilder().method("differentMethod").args("m").location("-> at M.java").toInvocation());
    // when
    mismatches.format("MyTest.myTestMethod", logger);
    // then
    assertEquals("[MockitoHint] MyTest.myTestMethod (see javadoc for MockitoHint):\n" + "[MockitoHint] 1. Unused... -> at A.java\n" + "[MockitoHint]  ...args ok? -> at X.java\n" + "[MockitoHint]  ...args ok? -> at Y.java\n" + "[MockitoHint] 2. Unused... -> at M.java\n" + "[MockitoHint]  ...args ok? -> at N.java\n", logger.getLoggedInfo());
}
Also used : Invocation(org.mockito.invocation.Invocation) InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) Test(org.junit.Test)

Aggregations

InvocationBuilder (org.mockito.internal.invocation.InvocationBuilder)55 Test (org.junit.Test)54 Invocation (org.mockito.invocation.Invocation)36 InvocationMatcher (org.mockito.internal.invocation.InvocationMatcher)15 Returns (org.mockito.internal.stubbing.answers.Returns)9 MockitoException (org.mockito.exceptions.base.MockitoException)7 VerificationInOrderFailure (org.mockito.exceptions.verification.VerificationInOrderFailure)5 MockSettingsImpl (org.mockito.internal.creation.MockSettingsImpl)5 VerificationDataInOrderImpl (org.mockito.internal.verification.api.VerificationDataInOrderImpl)5 IMethods (org.mockitousage.IMethods)5 WrongTypeOfReturnValue (org.mockito.exceptions.misusing.WrongTypeOfReturnValue)4 NoInteractionsWanted (org.mockito.exceptions.verification.NoInteractionsWanted)3 InOrderContextImpl (org.mockito.internal.verification.InOrderContextImpl)3 InOrderContext (org.mockito.internal.verification.api.InOrderContext)3 CharacterCodingException (java.nio.charset.CharacterCodingException)2 InvocationContainerImpl (org.mockito.internal.stubbing.InvocationContainerImpl)2 ThrowsException (org.mockito.internal.stubbing.answers.ThrowsException)2 ReturnsEmptyValues (org.mockito.internal.stubbing.defaultanswers.ReturnsEmptyValues)2 MatchableInvocation (org.mockito.invocation.MatchableInvocation)2 Date (java.util.Date)1