Search in sources :

Example 1 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 2 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 3 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 4 with InvocationBuilder

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

the class AtLeastXNumberOfInvocationsCheckerTest method shouldReportTooLittleInvocationsInOrder.

@Test
public void shouldReportTooLittleInvocationsInOrder() {
    InOrderContext context = new InOrderContextImpl();
    //given
    Invocation invocation = new InvocationBuilder().simpleMethod().toInvocation();
    Invocation invocationTwo = new InvocationBuilder().differentMethod().toInvocation();
    exception.expect(VerificationInOrderFailure.class);
    exception.expectMessage("iMethods.simpleMethod()");
    exception.expectMessage("Wanted *at least* 2 times");
    exception.expectMessage("But was 1 time");
    //when
    checkAtLeastNumberOfInvocations(asList(invocation, invocationTwo), new InvocationMatcher(invocation), 2, context);
}
Also used : InOrderContextImpl(org.mockito.internal.verification.InOrderContextImpl) Invocation(org.mockito.invocation.Invocation) InvocationMatcher(org.mockito.internal.invocation.InvocationMatcher) InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) InOrderContext(org.mockito.internal.verification.api.InOrderContext) Test(org.junit.Test)

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

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