Search in sources :

Example 21 with IMethods

use of org.mockitousage.IMethods in project mockito by mockito.

the class BasicStubbingTest method test_stub_only_not_verifiable.

@Test
public void test_stub_only_not_verifiable() throws Exception {
    IMethods localMock = mock(IMethods.class, withSettings().stubOnly());
    when(localMock.objectReturningMethod(isA(Integer.class))).thenReturn(100);
    when(localMock.objectReturningMethod(200)).thenReturn(200);
    assertEquals(200, localMock.objectReturningMethod(200));
    assertEquals(100, localMock.objectReturningMethod(666));
    assertEquals("default behavior should return null", null, localMock.objectReturningMethod("blah"));
    try {
        verify(localMock, atLeastOnce()).objectReturningMethod(eq(200));
        fail();
    } catch (CannotVerifyStubOnlyMock e) {
    }
}
Also used : CannotVerifyStubOnlyMock(org.mockito.exceptions.misusing.CannotVerifyStubOnlyMock) IMethods(org.mockitousage.IMethods) Test(org.junit.Test)

Example 22 with IMethods

use of org.mockitousage.IMethods in project mockito by mockito.

the class StubbingWithAdditionalAnswersTest method will_execute_a_void_based_on_strongly_typed_one_parameter_function.

@Test
public void will_execute_a_void_based_on_strongly_typed_one_parameter_function() throws Exception {
    final IMethods target = mock(IMethods.class);
    given(iMethods.simpleMethod(anyString())).will(answerVoid(new VoidAnswer1<String>() {

        public void answer(String s) {
            target.simpleMethod(s);
        }
    }));
    // invoke on iMethods
    iMethods.simpleMethod("string");
    // expect the answer to write correctly to "target"
    verify(target, times(1)).simpleMethod("string");
}
Also used : VoidAnswer1(org.mockito.stubbing.VoidAnswer1) IMethods(org.mockitousage.IMethods) BDDMockito.anyString(org.mockito.BDDMockito.anyString) Test(org.junit.Test)

Example 23 with IMethods

use of org.mockitousage.IMethods in project mockito by mockito.

the class StubbingWithDelegateTest method null_wrapper_dont_throw_exception_from_org_mockito_package.

@Test
public void null_wrapper_dont_throw_exception_from_org_mockito_package() throws Exception {
    IMethods methods = mock(IMethods.class, delegatesTo(new MethodsImpl()));
    try {
        // real method returns null
        byte b = methods.byteObjectReturningMethod();
        fail();
    } catch (Exception e) {
        assertThat(e.toString()).doesNotContain("org.mockito");
    }
}
Also used : IMethods(org.mockitousage.IMethods) MethodsImpl(org.mockitousage.MethodsImpl) MockitoException(org.mockito.exceptions.base.MockitoException) Test(org.junit.Test)

Example 24 with IMethods

use of org.mockitousage.IMethods in project mockito by mockito.

the class PrintingVerboseTypesWithArgumentsTest method should_not_show_types_when_types_are_the_same_even_if_to_string_gives_the_same_result.

@Test
public void should_not_show_types_when_types_are_the_same_even_if_to_string_gives_the_same_result() {
    //given
    IMethods mock = mock(IMethods.class);
    mock.simpleMethod(new Foo(10));
    try {
        //when
        verify(mock).simpleMethod(new Foo(20));
        fail();
    } catch (ArgumentsAreDifferent e) {
        //then
        assertThat(e).hasMessageContaining("simpleMethod(foo)");
    }
}
Also used : ArgumentsAreDifferent(org.mockito.exceptions.verification.junit.ArgumentsAreDifferent) IMethods(org.mockitousage.IMethods) Test(org.junit.Test)

Example 25 with IMethods

use of org.mockitousage.IMethods in project mockito by mockito.

the class SmartNullsStubbingTest method shouldSmartNPEPointToUnstubbedCall.

@Test
public void shouldSmartNPEPointToUnstubbedCall() throws Exception {
    IMethods methods = unstubbedMethodInvokedHere(mock);
    try {
        methods.simpleMethod();
        fail();
    } catch (SmartNullPointerException e) {
        assertThat(e).hasMessageContaining("unstubbedMethodInvokedHere(");
    }
}
Also used : IMethods(org.mockitousage.IMethods) SmartNullPointerException(org.mockito.exceptions.verification.SmartNullPointerException) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)64 IMethods (org.mockitousage.IMethods)64 ByteArrayOutputStream (java.io.ByteArrayOutputStream)22 BDDMockito.anyString (org.mockito.BDDMockito.anyString)8 VerificationCollector (org.mockito.junit.VerificationCollector)5 InvocationBuilder (org.mockito.internal.invocation.InvocationBuilder)4 Invocation (org.mockito.invocation.Invocation)3 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 InOrder (org.mockito.InOrder)2 SmartNullPointerException (org.mockito.exceptions.verification.SmartNullPointerException)2 Returns (org.mockito.internal.stubbing.answers.Returns)2 VoidAnswer3 (org.mockito.stubbing.VoidAnswer3)2 VoidAnswer4 (org.mockito.stubbing.VoidAnswer4)2 VoidAnswer5 (org.mockito.stubbing.VoidAnswer5)2 MethodsImpl (org.mockitousage.MethodsImpl)2 CharacterCodingException (java.nio.charset.CharacterCodingException)1 Ignore (org.junit.Ignore)1 MockitoAssertionError (org.mockito.exceptions.base.MockitoAssertionError)1