Search in sources :

Example 36 with IMethods

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

the class MocksSerializationForAnnotationTest method should_verify_even_if_some_methods_called_after_serialization.

@Test
public void should_verify_even_if_some_methods_called_after_serialization() throws Exception {
    // when
    imethodsMock.simpleMethod(1);
    ByteArrayOutputStream serialized = serializeMock(imethodsMock);
    IMethods readObject = deserializeMock(serialized, IMethods.class);
    readObject.simpleMethod(1);
    // then
    verify(readObject, times(2)).simpleMethod(1);
//this test is working because it seems that java serialization mechanism replaces all instances
//of serialized object in the object graph (if there are any)
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) IMethods(org.mockitousage.IMethods) Test(org.junit.Test)

Example 37 with IMethods

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

the class MocksSerializationForAnnotationTest method should_allow_mock_and_string_value_to_be_serializable.

@Test
public void should_allow_mock_and_string_value_to_be_serializable() throws Exception {
    // given
    String value = "value";
    when(imethodsMock.stringReturningMethod()).thenReturn(value);
    // when
    ByteArrayOutputStream serialized = serializeMock(imethodsMock);
    // then
    IMethods readObject = deserializeMock(serialized, IMethods.class);
    assertEquals(value, readObject.stringReturningMethod());
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) IMethods(org.mockitousage.IMethods) Test(org.junit.Test)

Example 38 with IMethods

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

the class MocksSerializationTest method should_allow_mock_and_string_value_to_be_serializable.

@Test
public void should_allow_mock_and_string_value_to_be_serializable() throws Exception {
    // given
    IMethods mock = mock(IMethods.class, withSettings().serializable());
    String value = "value";
    when(mock.stringReturningMethod()).thenReturn(value);
    // when
    ByteArrayOutputStream serialized = serializeMock(mock);
    // then
    IMethods readObject = deserializeMock(serialized, IMethods.class);
    assertEquals(value, readObject.stringReturningMethod());
}
Also used : IMethods(org.mockitousage.IMethods) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 39 with IMethods

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

the class MocksSerializationTest method should_serialize_with_stubbing_callback.

@Test
public void should_serialize_with_stubbing_callback() throws Exception {
    // given
    IMethods mock = mock(IMethods.class, withSettings().serializable());
    CustomAnswersMustImplementSerializableForSerializationToWork answer = new CustomAnswersMustImplementSerializableForSerializationToWork();
    answer.string = "return value";
    when(mock.objectArgMethod(anyString())).thenAnswer(answer);
    // when
    ByteArrayOutputStream serialized = serializeMock(mock);
    // then
    IMethods readObject = deserializeMock(serialized, IMethods.class);
    assertEquals(answer.string, readObject.objectArgMethod(""));
}
Also used : IMethods(org.mockitousage.IMethods) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 40 with IMethods

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

the class MocksSerializationTest method should_allow_mock_and_boolean_value_to_serializable.

@Test
public void should_allow_mock_and_boolean_value_to_serializable() throws Exception {
    // given
    IMethods mock = mock(IMethods.class, withSettings().serializable());
    when(mock.booleanReturningMethod()).thenReturn(true);
    // when
    ByteArrayOutputStream serialized = serializeMock(mock);
    // then
    IMethods readObject = deserializeMock(serialized, IMethods.class);
    assertTrue(readObject.booleanReturningMethod());
}
Also used : IMethods(org.mockitousage.IMethods) ByteArrayOutputStream(java.io.ByteArrayOutputStream) 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