Search in sources :

Example 46 with InvocationBuilder

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

the class ReporterTest method can_use_print_mock_name_even_when_mock_bogus_default_answer_and_when_reporting_delegate_method_has_wrong_return_type.

@Test
public void can_use_print_mock_name_even_when_mock_bogus_default_answer_and_when_reporting_delegate_method_has_wrong_return_type() {
    Invocation dumb_invocation = new InvocationBuilder().toInvocation();
    IMethods mock_with_bogus_default_answer = mock(IMethods.class, new Returns(false));
    assertThatThrownBy(() -> {
        throw Reporter.delegatedMethodHasWrongReturnType(dumb_invocation.getMethod(), dumb_invocation.getMethod(), mock_with_bogus_default_answer, String.class);
    }).isInstanceOf(MockitoException.class).hasMessageContainingAll("Methods called on delegated instance must have compatible return types with the mock.", "When calling: public abstract java.lang.String org.mockitousage.IMethods.simpleMethod() on mock: iMethods", "return type should be: String, but was: String", "Check that the instance passed to delegatesTo() is of the correct type or contains compatible methods", "(delegate instance had type: Class)");
}
Also used : Returns(org.mockito.internal.stubbing.answers.Returns) Invocation(org.mockito.invocation.Invocation) MockitoException(org.mockito.exceptions.base.MockitoException) InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) IMethods(org.mockitousage.IMethods) Test(org.junit.Test)

Example 47 with InvocationBuilder

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

the class ReporterTest method can_use_print_mock_name_even_when_mock_bogus_default_answer_and_when_reporting_invalid_argument_position.

@Test
public void can_use_print_mock_name_even_when_mock_bogus_default_answer_and_when_reporting_invalid_argument_position() {
    Invocation invocation_with_bogus_default_answer = new InvocationBuilder().mock(mock(IMethods.class, new Returns(false))).toInvocation();
    assertThatThrownBy(() -> {
        throw Reporter.invalidArgumentPositionRangeAtInvocationTime(invocation_with_bogus_default_answer, true, 0);
    }).isInstanceOf(MockitoException.class).hasMessageContainingAll("Invalid argument index for the current invocation of method :", " -> iMethods.simpleMethod()", "Last parameter wanted but the method has no arguments.");
}
Also used : Returns(org.mockito.internal.stubbing.answers.Returns) Invocation(org.mockito.invocation.Invocation) MockitoException(org.mockito.exceptions.base.MockitoException) InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) Test(org.junit.Test)

Example 48 with InvocationBuilder

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

the class ReporterTest method can_use_print_mock_name_even_when_mock_bogus_default_answer_and_when_reporting_delegate_method_dont_exists.

@Test
public void can_use_print_mock_name_even_when_mock_bogus_default_answer_and_when_reporting_delegate_method_dont_exists() {
    Invocation dumb_invocation = new InvocationBuilder().toInvocation();
    IMethods mock_with_bogus_default_answer = mock(IMethods.class, new Returns(false));
    assertThatThrownBy(() -> {
        throw Reporter.delegatedMethodDoesNotExistOnDelegate(dumb_invocation.getMethod(), mock_with_bogus_default_answer, String.class);
    }).isInstanceOf(MockitoException.class).hasMessageContainingAll("Methods called on mock must exist in delegated instance.", "When calling: public abstract java.lang.String org.mockitousage.IMethods.simpleMethod() on mock: iMethods", "no such method was found.", "Check that the instance passed to delegatesTo() is of the correct type or contains compatible methods", "(delegate instance had type: Class)");
}
Also used : Returns(org.mockito.internal.stubbing.answers.Returns) Invocation(org.mockito.invocation.Invocation) MockitoException(org.mockito.exceptions.base.MockitoException) InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) IMethods(org.mockitousage.IMethods) Test(org.junit.Test)

Example 49 with InvocationBuilder

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

the class InvocationInfoExceptionTest method should_know_valid_throwables.

@Test
public void should_know_valid_throwables() throws Exception {
    // when
    final Invocation invocation = new InvocationBuilder().method(methodName).mockClass(CurrentClass.class).toInvocation();
    final 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) InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) CharacterCodingException(java.nio.charset.CharacterCodingException) CharacterCodingException(java.nio.charset.CharacterCodingException) Test(org.junit.Test)

Example 50 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)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