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));
}
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));
}
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());
}
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);
}
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();
}
}
}
Aggregations