Search in sources :

Example 16 with IMethods

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

the class MocksSerializationForAnnotationTest method should_stub_even_if_some_methods_called_after_serialization.

@Test
public void should_stub_even_if_some_methods_called_after_serialization() throws Exception {
    //given
    // when
    when(imethodsMock.simpleMethod(1)).thenReturn("foo");
    ByteArrayOutputStream serialized = serializeMock(imethodsMock);
    IMethods readObject = deserializeMock(serialized, IMethods.class);
    when(readObject.simpleMethod(2)).thenReturn("bar");
    // then
    assertEquals("foo", readObject.simpleMethod(1));
    assertEquals("bar", readObject.simpleMethod(2));
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) IMethods(org.mockitousage.IMethods) Test(org.junit.Test)

Example 17 with IMethods

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

the class MocksSerializationForAnnotationTest method should_serialize_method_call_with_parameters_that_are_serializable.

@Test
public void should_serialize_method_call_with_parameters_that_are_serializable() throws Exception {
    List<?> value = Collections.emptyList();
    when(imethodsMock.objectArgMethod(value)).thenReturn(value);
    // when
    ByteArrayOutputStream serialized = serializeMock(imethodsMock);
    // then
    IMethods readObject = deserializeMock(serialized, IMethods.class);
    assertEquals(value, readObject.objectArgMethod(value));
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) IMethods(org.mockitousage.IMethods) Test(org.junit.Test)

Example 18 with IMethods

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

the class MocksSerializationForAnnotationTest method should_allow_mock_and_boolean_value_to_serializable.

@Test
public void should_allow_mock_and_boolean_value_to_serializable() throws Exception {
    // given
    when(imethodsMock.booleanReturningMethod()).thenReturn(true);
    // when
    ByteArrayOutputStream serialized = serializeMock(imethodsMock);
    // then
    IMethods readObject = deserializeMock(serialized, IMethods.class);
    assertTrue(readObject.booleanReturningMethod());
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) IMethods(org.mockitousage.IMethods) Test(org.junit.Test)

Example 19 with IMethods

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

the class MocksSerializationTest method BUG_ISSUE_399_try_some_mocks_with_current_answers.

@Test
public void BUG_ISSUE_399_try_some_mocks_with_current_answers() throws Exception {
    // Bug in last public HotSpot 1.6
    assumeFalse(System.getProperty("java.version").startsWith("1.6"));
    IMethods iMethods = mock(IMethods.class, withSettings().serializable().defaultAnswer(RETURNS_DEEP_STUBS));
    when(iMethods.iMethodsReturningMethod().linkedListReturningMethod().contains(anyString())).thenReturn(false);
    serializeAndBack(iMethods);
}
Also used : IMethods(org.mockitousage.IMethods) Test(org.junit.Test)

Example 20 with IMethods

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

the class ParallelSerializationTest method single_mock_being_serialized_in_different_classloaders_by_multiple_threads.

@Test
public void single_mock_being_serialized_in_different_classloaders_by_multiple_threads() throws ExecutionException, InterruptedException {
    // given
    int iterations = 2;
    int threadingFactor = 200;
    final ExecutorService executorService = Executors.newFixedThreadPool(threadingFactor);
    final IMethods iMethods_that_store_invocations = mock(IMethods.class, withSettings().serializable());
    // when
    for (int i = 0; i <= iterations; i++) {
        List<Future<?>> futures = new ArrayList<Future<?>>(threadingFactor);
        final CyclicBarrier barrier_that_will_wait_until_threads_are_ready = new CyclicBarrier(threadingFactor);
        //  - that will use the mock a 'threadingFactor' times
        for (int j = 0; j < threadingFactor; j++) {
            // submit a callable that will serialize the mock 'iMethods'
            futures.add(executorService.submit(new Callable<Object>() {

                public Object call() throws Exception {
                    barrier_that_will_wait_until_threads_are_ready.await();
                    randomCallOn(iMethods_that_store_invocations);
                    return SimpleSerializationUtil.serializeMock(iMethods_that_store_invocations).toByteArray();
                }
            }));
            // submit a callable that will only use the mock 'iMethods'
            executorService.submit(new Callable<Object>() {

                public Object call() throws Exception {
                    barrier_that_will_wait_until_threads_are_ready.await();
                    return iMethods_that_store_invocations.longObjectReturningMethod();
                }
            });
        }
        // ensure we are getting the futures
        for (Future<?> future : futures) {
            future.get();
        }
    }
}
Also used : ArrayList(java.util.ArrayList) IMethods(org.mockitousage.IMethods) CharacterCodingException(java.nio.charset.CharacterCodingException) 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