Search in sources :

Example 46 with IMethods

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

the class StubbingMocksThatAreConfiguredToReturnMocksTest method shouldAllowStubbingMocksConfiguredWithRETURNS_MOCKS.

@Test
public void shouldAllowStubbingMocksConfiguredWithRETURNS_MOCKS() {
    IMethods mock = mock(IMethods.class, RETURNS_MOCKS);
    when(mock.objectReturningMethodNoArgs()).thenReturn(null);
}
Also used : IMethods(org.mockitousage.IMethods) Test(org.junit.Test)

Example 47 with IMethods

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

the class LoadsOfMocksTest method testSomething.

@Ignore("Use it for performance checks")
@Test
public void testSomething() {
    List<IMethods> mocks = new LinkedList<IMethods>();
    for (int i = 0; i < 50000; i++) {
        System.out.println("Mock no: " + i);
        IMethods mock = mock(IMethods.class);
        mocks.add(mock);
        when(mock.simpleMethod(1)).thenReturn("one");
        when(mock.simpleMethod(2)).thenReturn("two");
        assertEquals("one", mock.simpleMethod(1));
        assertEquals("two", mock.simpleMethod(2));
        verify(mock).simpleMethod(1);
        verify(mock).simpleMethod(2);
    }
}
Also used : IMethods(org.mockitousage.IMethods) LinkedList(java.util.LinkedList) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 48 with IMethods

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

the class SmartNullsStubbingTest method shouldReturnOrdinaryEmptyValuesForOrdinaryTypes.

@Test
public void shouldReturnOrdinaryEmptyValuesForOrdinaryTypes() throws Exception {
    IMethods mock = mock(IMethods.class, RETURNS_SMART_NULLS);
    assertEquals("", mock.stringReturningMethod());
    assertEquals(0, mock.intReturningMethod());
    assertEquals(true, mock.listReturningMethod().isEmpty());
    assertEquals(0, mock.arrayReturningMethod().length);
}
Also used : IMethods(org.mockitousage.IMethods) Test(org.junit.Test)

Example 49 with IMethods

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

the class StubbingWithAdditionalAnswersTest method can_return_based_on_strongly_typed_five_parameter_function.

@Test
public void can_return_based_on_strongly_typed_five_parameter_function() throws Exception {
    final IMethods target = mock(IMethods.class);
    given(iMethods.simpleMethod(anyString(), anyInt(), anyInt(), anyInt(), anyInt())).will(answer(new Answer5<String, String, Integer, Integer, Integer, Integer>() {

        public String answer(String s1, Integer i1, Integer i2, Integer i3, Integer i4) {
            target.simpleMethod(s1, i1, i2, i3, i4);
            return "answered";
        }
    }));
    assertThat(iMethods.simpleMethod("hello", 1, 2, 3, 4)).isEqualTo("answered");
    verify(target, times(1)).simpleMethod("hello", 1, 2, 3, 4);
}
Also used : Answer5(org.mockito.stubbing.Answer5) VoidAnswer5(org.mockito.stubbing.VoidAnswer5) IMethods(org.mockitousage.IMethods) BDDMockito.anyString(org.mockito.BDDMockito.anyString) Test(org.junit.Test)

Example 50 with IMethods

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

the class StubbingWithAdditionalAnswersTest method will_execute_a_void_based_on_strongly_typed_four_parameter_function.

@Test
public void will_execute_a_void_based_on_strongly_typed_four_parameter_function() throws Exception {
    final IMethods target = mock(IMethods.class);
    given(iMethods.fourArgumentMethod(anyInt(), anyString(), anyString(), any(boolean[].class))).will(answerVoid(new VoidAnswer4<Integer, String, String, boolean[]>() {

        public void answer(Integer i, String s1, String s2, boolean[] a) {
            target.fourArgumentMethod(i, s1, s2, a);
        }
    }));
    // invoke on iMethods
    boolean[] booleanArray = { true, false };
    iMethods.fourArgumentMethod(1, "string1", "string2", booleanArray);
    // expect the answer to write correctly to "target"
    verify(target, times(1)).fourArgumentMethod(1, "string1", "string2", booleanArray);
}
Also used : IMethods(org.mockitousage.IMethods) VoidAnswer4(org.mockito.stubbing.VoidAnswer4) BDDMockito.anyString(org.mockito.BDDMockito.anyString) 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