Search in sources :

Example 26 with IMethods

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

the class StubbingWithAdditionalAnswersTest method will_execute_a_void_based_on_strongly_typed_two_parameter_function.

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

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

Example 27 with IMethods

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

the class StubbingWithAdditionalAnswersTest method can_return_based_on_strongly_typed_four_parameter_function.

@Test
public void can_return_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(answer(new Answer4<String, Integer, String, String, boolean[]>() {

        public String answer(Integer i, String s1, String s2, boolean[] a) {
            target.fourArgumentMethod(i, s1, s2, a);
            return "answered";
        }
    }));
    boolean[] booleanArray = { true, false };
    assertThat(iMethods.fourArgumentMethod(1, "string1", "string2", booleanArray)).isEqualTo("answered");
    verify(target, times(1)).fourArgumentMethod(1, "string1", "string2", booleanArray);
}
Also used : Answer4(org.mockito.stubbing.Answer4) VoidAnswer4(org.mockito.stubbing.VoidAnswer4) IMethods(org.mockitousage.IMethods) BDDMockito.anyString(org.mockito.BDDMockito.anyString) Test(org.junit.Test)

Example 28 with IMethods

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

the class StubbingWithAdditionalAnswersTest method can_return_based_on_strongly_typed_three_parameter_function.

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

        public String answer(Integer i, String s1, String s2) {
            target.threeArgumentMethodWithStrings(i, s1, s2);
            return "answered";
        }
    }));
    assertThat(iMethods.threeArgumentMethodWithStrings(1, "string1", "string2")).isEqualTo("answered");
    verify(target, times(1)).threeArgumentMethodWithStrings(1, "string1", "string2");
}
Also used : VoidAnswer3(org.mockito.stubbing.VoidAnswer3) Answer3(org.mockito.stubbing.Answer3) IMethods(org.mockitousage.IMethods) BDDMockito.anyString(org.mockito.BDDMockito.anyString) Test(org.junit.Test)

Example 29 with IMethods

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

the class StubbingWithAdditionalAnswersTest method will_execute_a_void_based_on_strongly_typed_five_parameter_function.

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

        public void answer(String s1, Integer i1, Integer i2, Integer i3, Integer i4) {
            target.simpleMethod(s1, i1, i2, i3, i4);
        }
    }));
    // invoke on iMethods
    iMethods.simpleMethod("hello", 1, 2, 3, 4);
    // expect the answer to write correctly to "target"
    verify(target, times(1)).simpleMethod("hello", 1, 2, 3, 4);
}
Also used : VoidAnswer5(org.mockito.stubbing.VoidAnswer5) IMethods(org.mockitousage.IMethods) BDDMockito.anyString(org.mockito.BDDMockito.anyString) Test(org.junit.Test)

Example 30 with IMethods

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

the class ReporterTest method can_use_print_mock_name_even_when_mock_bogus_default_answer_and_when_reporting_delegate_method_has_wrong_return_type.

@Test(expected = MockitoException.class)
public void can_use_print_mock_name_even_when_mock_bogus_default_answer_and_when_reporting_delegate_method_has_wrong_return_type() throws Exception {
    Invocation dumb_invocation = new InvocationBuilder().toInvocation();
    IMethods mock_with_bogus_default_answer = mock(IMethods.class, new Returns(false));
    throw Reporter.delegatedMethodHasWrongReturnType(dumb_invocation.getMethod(), dumb_invocation.getMethod(), mock_with_bogus_default_answer, String.class);
}
Also used : Returns(org.mockito.internal.stubbing.answers.Returns) Invocation(org.mockito.invocation.Invocation) InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) IMethods(org.mockitousage.IMethods) 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