Search in sources :

Example 11 with InvocationBuilder

use of org.mockito.internal.invocation.InvocationBuilder 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 12 with InvocationBuilder

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

the class ReturnsArgumentAtTest method should_allow_possible_argument_types.

@Test
public void should_allow_possible_argument_types() throws Exception {
    new ReturnsArgumentAt(0).validateFor(new InvocationBuilder().method("intArgumentReturningInt").argTypes(int.class).arg(1000).toInvocation());
    new ReturnsArgumentAt(0).validateFor(new InvocationBuilder().method("toString").argTypes(String.class).arg("whatever").toInvocation());
    new ReturnsArgumentAt(2).validateFor(new InvocationBuilder().method("varargsObject").argTypes(int.class, Object[].class).args(1000, "Object", "Object").toInvocation());
    new ReturnsArgumentAt(1).validateFor(new InvocationBuilder().method("threeArgumentMethod").argTypes(int.class, Object.class, String.class).args(1000, "Object", "String").toInvocation());
}
Also used : InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) Test(org.junit.Test)

Example 13 with InvocationBuilder

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

the class ThrowsExceptionTest method should_raise_wanted_throwable.

@Test
public void should_raise_wanted_throwable() throws Throwable {
    try {
        new ThrowsException(new IllegalStateException("my dear throwable")).answer(new InvocationBuilder().method("canThrowException").toInvocation());
        Assertions.fail("should have raised wanted exception");
    } catch (Throwable throwable) {
        assertThat(throwable).isInstanceOf(IllegalStateException.class).hasMessage("my dear throwable");
    }
}
Also used : InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) Test(org.junit.Test)

Example 14 with InvocationBuilder

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

the class ThrowsExceptionTest method should_throw_mock_exception_without_stacktrace.

@Test
public void should_throw_mock_exception_without_stacktrace() throws Exception {
    try {
        new ThrowsException(mock(Exception.class)).answer(new InvocationBuilder().method("canThrowException").toInvocation());
        Assertions.fail("should have raised wanted exception");
    } catch (Throwable throwable) {
        assertThat(throwable.getStackTrace()).describedAs("no stack trace, it's mock").isNull();
    }
}
Also used : InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) Test(org.junit.Test)

Example 15 with InvocationBuilder

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

the class DefaultRegisteredInvocationsTest method should_not_return_to_string_method.

@Test
public void should_not_return_to_string_method() throws Exception {
    Invocation toString = new InvocationBuilder().method("toString").toInvocation();
    Invocation simpleMethod = new InvocationBuilder().simpleMethod().toInvocation();
    invocations.add(toString);
    invocations.add(simpleMethod);
    assertTrue(invocations.getAll().contains(simpleMethod));
    assertFalse(invocations.getAll().contains(toString));
}
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