Search in sources :

Example 36 with InvocationBuilder

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

the class CallsRealMethodsTest method should_delegate_to_returns_default_when_abstract_method.

@Test
public void should_delegate_to_returns_default_when_abstract_method() throws Throwable {
    Invocation abstractMethod = new InvocationBuilder().method("booleanReturningMethod").toInvocation();
    assertThat(new CallsRealMethods().answer(abstractMethod)).isEqualTo(RETURNS_DEFAULTS.answer(abstractMethod));
}
Also used : Invocation(org.mockito.invocation.Invocation) InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) Test(org.junit.Test)

Example 37 with InvocationBuilder

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

the class InvocationInfoTest method should_know_valid_throwables.

@Test
public void should_know_valid_throwables() throws Exception {
    //when
    Invocation invocation = new InvocationBuilder().method("canThrowException").toInvocation();
    InvocationInfo info = new InvocationInfo(invocation);
    //then
    assertThat(info.isValidException(new Exception())).isFalse();
    assertThat(info.isValidException(new CharacterCodingException())).isTrue();
}
Also used : Invocation(org.mockito.invocation.Invocation) TestBase.getLastInvocation(org.mockitoutil.TestBase.getLastInvocation) InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) CharacterCodingException(java.nio.charset.CharacterCodingException) CharacterCodingException(java.nio.charset.CharacterCodingException) Test(org.junit.Test)

Example 38 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 39 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 40 with InvocationBuilder

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

the class ReturnsArgumentAtTest method should_handle_returning_vararg_as_array.

@Test
public void should_handle_returning_vararg_as_array() throws Throwable {
    Invocation mixedVarargsReturningStringArray = new InvocationBuilder().method("mixedVarargsReturningStringArray").argTypes(Object.class, String[].class).args(new Object(), new String[] { "A", "B", "C" }).toInvocation();
    new ReturnsArgumentAt(1).validateFor(mixedVarargsReturningStringArray);
    assertThat(new ReturnsArgumentAt(1).answer(mixedVarargsReturningStringArray)).isEqualTo(new String[] { "A", "B", "C" });
    Invocation mixedVarargsReturningObjectArray = new InvocationBuilder().method("mixedVarargsReturningStringArray").argTypes(Object.class, String[].class).args(new Object(), new String[] { "A", "B", "C" }).toInvocation();
    new ReturnsArgumentAt(1).validateFor(mixedVarargsReturningObjectArray);
    assertThat(new ReturnsArgumentAt(1).answer(mixedVarargsReturningObjectArray)).isEqualTo(new String[] { "A", "B", "C" });
}
Also used : Invocation(org.mockito.invocation.Invocation) InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) Test(org.junit.Test)

Aggregations

InvocationBuilder (org.mockito.internal.invocation.InvocationBuilder)46 Test (org.junit.Test)45 Invocation (org.mockito.invocation.Invocation)28 InvocationMatcher (org.mockito.internal.invocation.InvocationMatcher)12 Returns (org.mockito.internal.stubbing.answers.Returns)5 VerificationDataInOrderImpl (org.mockito.internal.verification.api.VerificationDataInOrderImpl)5 MockitoException (org.mockito.exceptions.base.MockitoException)4 MockSettingsImpl (org.mockito.internal.creation.MockSettingsImpl)4 IMethods (org.mockitousage.IMethods)4 WrongTypeOfReturnValue (org.mockito.exceptions.misusing.WrongTypeOfReturnValue)3 VerificationInOrderFailure (org.mockito.exceptions.verification.VerificationInOrderFailure)3 CharacterCodingException (java.nio.charset.CharacterCodingException)2 StubbedInvocationMatcher (org.mockito.internal.stubbing.StubbedInvocationMatcher)2 ThrowsException (org.mockito.internal.stubbing.answers.ThrowsException)2 ReturnsEmptyValues (org.mockito.internal.stubbing.defaultanswers.ReturnsEmptyValues)2 InOrderContextImpl (org.mockito.internal.verification.InOrderContextImpl)2 InOrderContext (org.mockito.internal.verification.api.InOrderContext)2 MatchableInvocation (org.mockito.invocation.MatchableInvocation)2 IOException (java.io.IOException)1 Before (org.junit.Before)1