Search in sources :

Example 1 with WrongTypeOfReturnValue

use of org.mockito.exceptions.misusing.WrongTypeOfReturnValue in project mockito by mockito.

the class DefaultAnswerValidatorTest method should_fail_if_returned_value_of_answer_is_incompatible_with_return_type.

@Test
public void should_fail_if_returned_value_of_answer_is_incompatible_with_return_type() throws Throwable {
    // given
    class AWrongType {
    }
    try {
        // when
        DefaultAnswerValidator.validateReturnValueFor(new InvocationBuilder().method("toString").toInvocation(), new AWrongType());
        fail("expected validation to fail");
    } catch (WrongTypeOfReturnValue e) {
        // then
        assertThat(e.getMessage()).containsIgnoringCase("Default answer returned a result with the wrong type").containsIgnoringCase("AWrongType cannot be returned by toString()").containsIgnoringCase("toString() should return String");
    }
}
Also used : WrongTypeOfReturnValue(org.mockito.exceptions.misusing.WrongTypeOfReturnValue) InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) Test(org.junit.Test)

Example 2 with WrongTypeOfReturnValue

use of org.mockito.exceptions.misusing.WrongTypeOfReturnValue in project mockito by mockito.

the class ReturnsArgumentAtTest method should_fail_if_argument_type_of_signature_is_incompatible_with_return_type.

@Test
public void should_fail_if_argument_type_of_signature_is_incompatible_with_return_type() throws Throwable {
    try {
        new ReturnsArgumentAt(2).validateFor(new InvocationBuilder().method("varargsReturningString").argTypes(Object[].class).args("anyString", new Object(), "anyString").toInvocation());
        fail();
    } catch (WrongTypeOfReturnValue e) {
        assertThat(e.getMessage()).containsIgnoringCase("argument of type").containsIgnoringCase("Object").containsIgnoringCase("varargsReturningString").containsIgnoringCase("should return").containsIgnoringCase("String").containsIgnoringCase("possible argument indexes");
    }
}
Also used : WrongTypeOfReturnValue(org.mockito.exceptions.misusing.WrongTypeOfReturnValue) InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) Test(org.junit.Test)

Example 3 with WrongTypeOfReturnValue

use of org.mockito.exceptions.misusing.WrongTypeOfReturnValue 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)

Aggregations

Test (org.junit.Test)3 WrongTypeOfReturnValue (org.mockito.exceptions.misusing.WrongTypeOfReturnValue)3 InvocationBuilder (org.mockito.internal.invocation.InvocationBuilder)3