Search in sources :

Example 6 with InvocationBuilder

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

the class WarningsFinderTest method shouldPrintUnstubbedInvocation.

@Test
public void shouldPrintUnstubbedInvocation() {
    // given
    InvocationMatcher unstubbedInvocation = new InvocationBuilder().differentMethod().toInvocationMatcher();
    // when
    WarningsFinder finder = new WarningsFinder(Arrays.<Invocation>asList(), Arrays.<InvocationMatcher>asList(unstubbedInvocation));
    finder.find(listener);
    // then
    verify(listener, only()).foundUnstubbed(unstubbedInvocation);
}
Also used : InvocationMatcher(org.mockito.internal.invocation.InvocationMatcher) InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) Test(org.junit.Test)

Example 7 with InvocationBuilder

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

the class ScenarioPrinterTest method shouldNotPrintInvocationsWhenSingleUnwanted.

@Test
public void shouldNotPrintInvocationsWhenSingleUnwanted() {
    //given
    Invocation unverified = new InvocationBuilder().differentMethod().toInvocation();
    //when
    String out = sp.print((List) asList(unverified));
    //then
    assertThat(out).contains("Actually, above is the only interaction with this mock.");
}
Also used : Invocation(org.mockito.invocation.Invocation) InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) Test(org.junit.Test)

Example 8 with InvocationBuilder

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

the class CallsRealMethodsTest method should_fail_when_calling_real_method_on_interface.

@Test
public void should_fail_when_calling_real_method_on_interface() throws Throwable {
    //given
    Invocation invocationOnInterface = new InvocationBuilder().method("simpleMethod").toInvocation();
    try {
        //when
        new CallsRealMethods().validateFor(invocationOnInterface);
        //then
        Assertions.fail("can not invoke interface");
    } catch (MockitoException expected) {
    }
}
Also used : Invocation(org.mockito.invocation.Invocation) MockitoException(org.mockito.exceptions.base.MockitoException) InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) Test(org.junit.Test)

Example 9 with InvocationBuilder

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

the class CallsRealMethodsTest method should_invoke_real_method.

@Test
public void should_invoke_real_method() throws Throwable {
    class Concrete {

        void concrete() {
            throw new RuntimeException("real code");
        }
    }
    Concrete mock = mock(Concrete.class);
    Invocation concrete = new InvocationBuilder().mock(mock).method(Concrete.class.getDeclaredMethod("concrete")).toInvocation();
    try {
        new CallsRealMethods().answer(concrete);
    } catch (RuntimeException throwable) {
        throwable.printStackTrace();
        assertThat(throwable).hasMessage("real code");
    }
}
Also used : Invocation(org.mockito.invocation.Invocation) InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) Test(org.junit.Test)

Example 10 with InvocationBuilder

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

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