How to use serializeAndBack method of org.mockitoutil.SimpleSerializationUtil class

Best Mockito code snippet using org.mockitoutil.SimpleSerializationUtil.serializeAndBack

Source:MocksSerializationTest.java Github

copy

Full Screen

...14import static org.mockito.Mockito.verify;15import static org.mockito.Mockito.when;16import static org.mockito.Mockito.withSettings;17import static org.mockitoutil.SimpleSerializationUtil.deserializeMock;18import static org.mockitoutil.SimpleSerializationUtil.serializeAndBack;19import static org.mockitoutil.SimpleSerializationUtil.serializeMock;20import java.io.ByteArrayOutputStream;21import java.io.ObjectStreamException;22import java.io.Serializable;23import java.util.ArrayList;24import java.util.Collections;25import java.util.List;26import java.util.Observable;27import org.fest.assertions.Assertions;28import org.junit.Test;29import org.mockito.InOrder;30import org.mockito.Mockito;31import org.mockito.exceptions.base.MockitoException;32import org.mockito.internal.matchers.Any;33import org.mockito.internal.stubbing.answers.ThrowsException;34import org.mockito.invocation.InvocationOnMock;35import org.mockito.stubbing.Answer;36import org.mockitousage.IMethods;37import org.mockitoutil.SimpleSerializationUtil;38import org.mockitoutil.TestBase;39@SuppressWarnings({"unchecked", "serial"})40public class MocksSerializationTest extends TestBase implements Serializable {41 private static final long serialVersionUID = 6160482220413048624L;42 @Test43 public void should_allow_throws_exception_to_be_serializable() throws Exception {44 // given45 Bar mock = mock(Bar.class, new ThrowsException(new RuntimeException()));46 // when-serialize then-deserialize47 serializeAndBack(mock);48 }49 @Test50 public void should_allow_method_delegation() throws Exception {51 // given52 Bar barMock = mock(Bar.class, withSettings().serializable());53 Foo fooMock = mock(Foo.class);54 when(barMock.doSomething()).thenAnswer(new ThrowsException(new RuntimeException()));55 //when-serialize then-deserialize56 serializeAndBack(barMock);57 }58 @Test59 public void should_allow_mock_to_be_serializable() throws Exception {60 // given61 IMethods mock = mock(IMethods.class, withSettings().serializable());62 // when-serialize then-deserialize63 serializeAndBack(mock);64 }65 @Test66 public void should_allow_mock_and_boolean_value_to_serializable() throws Exception {67 // given68 IMethods mock = mock(IMethods.class, withSettings().serializable());69 when(mock.booleanReturningMethod()).thenReturn(true);70 // when71 ByteArrayOutputStream serialized = serializeMock(mock);72 // then73 IMethods readObject = deserializeMock(serialized, IMethods.class);74 assertTrue(readObject.booleanReturningMethod());75 }76 @Test77 public void should_allow_mock_and_string_value_to_be_serializable() throws Exception {78 // given79 IMethods mock = mock(IMethods.class, withSettings().serializable());80 String value = "value";81 when(mock.stringReturningMethod()).thenReturn(value);82 // when83 ByteArrayOutputStream serialized = serializeMock(mock);84 // then85 IMethods readObject = deserializeMock(serialized, IMethods.class);86 assertEquals(value, readObject.stringReturningMethod());87 }88 @Test89 public void should_all_mock_and_serializable_value_to_be_serialized() throws Exception {90 // given91 IMethods mock = mock(IMethods.class, withSettings().serializable());92 List<?> value = Collections.emptyList();93 when(mock.objectReturningMethodNoArgs()).thenReturn(value);94 // when95 ByteArrayOutputStream serialized = serializeMock(mock);96 // then97 IMethods readObject = deserializeMock(serialized, IMethods.class);98 assertEquals(value, readObject.objectReturningMethodNoArgs());99 }100 @Test101 public void should_serialize_method_call_with_parameters_that_are_serializable() throws Exception {102 IMethods mock = mock(IMethods.class, withSettings().serializable());103 List<?> value = Collections.emptyList();104 when(mock.objectArgMethod(value)).thenReturn(value);105 // when106 ByteArrayOutputStream serialized = serializeMock(mock);107 // then108 IMethods readObject = deserializeMock(serialized, IMethods.class);109 assertEquals(value, readObject.objectArgMethod(value));110 }111 @Test112 public void should_serialize_method_calls_using_any_string_matcher() throws Exception {113 IMethods mock = mock(IMethods.class, withSettings().serializable());114 List<?> value = Collections.emptyList();115 when(mock.objectArgMethod(anyString())).thenReturn(value);116 // when117 ByteArrayOutputStream serialized = serializeMock(mock);118 // then119 IMethods readObject = deserializeMock(serialized, IMethods.class);120 assertEquals(value, readObject.objectArgMethod(""));121 }122 @Test123 public void should_verify_called_n_times_for_serialized_mock() throws Exception {124 IMethods mock = mock(IMethods.class, withSettings().serializable());125 List<?> value = Collections.emptyList();126 when(mock.objectArgMethod(anyString())).thenReturn(value);127 mock.objectArgMethod("");128 // when129 ByteArrayOutputStream serialized = serializeMock(mock);130 // then131 IMethods readObject = deserializeMock(serialized, IMethods.class);132 verify(readObject, times(1)).objectArgMethod("");133 }134 @Test135 public void should_verify_even_if_some_methods_called_after_serialization() throws Exception {136 //given137 IMethods mock = mock(IMethods.class, withSettings().serializable());138 // when139 mock.simpleMethod(1);140 ByteArrayOutputStream serialized = serializeMock(mock);141 IMethods readObject = deserializeMock(serialized, IMethods.class);142 readObject.simpleMethod(1);143 // then144 verify(readObject, times(2)).simpleMethod(1);145 //this test is working because it seems that java serialization mechanism replaces all instances146 //of serialized object in the object graph (if there are any)147 }148 class Bar implements Serializable {149 Foo foo;150 public Foo doSomething() {151 return foo;152 }153 }154 class Foo implements Serializable {155 Bar bar;156 Foo() {157 bar = new Bar();158 bar.foo = this;159 }160 }161 @Test162 public void should_serialization_work() throws Exception {163 //given164 Foo foo = new Foo();165 //when166 foo = serializeAndBack(foo);167 //then168 assertSame(foo, foo.bar.foo);169 }170 @Test171 public void should_stub_even_if_some_methods_called_after_serialization() throws Exception {172 //given173 IMethods mock = mock(IMethods.class, withSettings().serializable());174 // when175 when(mock.simpleMethod(1)).thenReturn("foo");176 ByteArrayOutputStream serialized = serializeMock(mock);177 IMethods readObject = deserializeMock(serialized, IMethods.class);178 when(readObject.simpleMethod(2)).thenReturn("bar");179 // then180 assertEquals("foo", readObject.simpleMethod(1));181 assertEquals("bar", readObject.simpleMethod(2));182 }183 @Test184 public void should_verify_call_order_for_serialized_mock() throws Exception {185 IMethods mock = mock(IMethods.class, withSettings().serializable());186 IMethods mock2 = mock(IMethods.class, withSettings().serializable());187 mock.arrayReturningMethod();188 mock2.arrayReturningMethod();189 // when190 ByteArrayOutputStream serialized = serializeMock(mock);191 ByteArrayOutputStream serialized2 = serializeMock(mock2);192 // then193 IMethods readObject = deserializeMock(serialized, IMethods.class);194 IMethods readObject2 = deserializeMock(serialized2, IMethods.class);195 InOrder inOrder = inOrder(readObject, readObject2);196 inOrder.verify(readObject).arrayReturningMethod();197 inOrder.verify(readObject2).arrayReturningMethod();198 }199 @Test200 public void should_remember_interactions_for_serialized_mock() throws Exception {201 IMethods mock = mock(IMethods.class, withSettings().serializable());202 List<?> value = Collections.emptyList();203 when(mock.objectArgMethod(anyString())).thenReturn(value);204 mock.objectArgMethod("happened");205 // when206 ByteArrayOutputStream serialized = serializeMock(mock);207 // then208 IMethods readObject = deserializeMock(serialized, IMethods.class);209 verify(readObject, never()).objectArgMethod("never happened");210 }211 @Test212 public void should_serialize_with_stubbing_callback() throws Exception {213 // given214 IMethods mock = mock(IMethods.class, withSettings().serializable());215 CustomAnswersMustImplementSerializableForSerializationToWork answer =216 new CustomAnswersMustImplementSerializableForSerializationToWork();217 answer.string = "return value";218 when(mock.objectArgMethod(anyString())).thenAnswer(answer);219 // when220 ByteArrayOutputStream serialized = serializeMock(mock);221 // then222 IMethods readObject = deserializeMock(serialized, IMethods.class);223 assertEquals(answer.string, readObject.objectArgMethod(""));224 }225 class CustomAnswersMustImplementSerializableForSerializationToWork226 implements Answer<Object>, Serializable {227 private String string;228 public Object answer(InvocationOnMock invocation) throws Throwable {229 invocation.getArguments();230 invocation.getMock();231 return string;232 }233 }234 @Test235 public void should_serialize_with_real_object_spy() throws Exception {236 // given237 List<Object> list = new ArrayList<Object>();238 List<Object> spy = mock(ArrayList.class, withSettings()239 .spiedInstance(list)240 .defaultAnswer(CALLS_REAL_METHODS)241 .serializable());242 when(spy.size()).thenReturn(100);243 // when244 ByteArrayOutputStream serialized = serializeMock(spy);245 // then246 List<?> readObject = deserializeMock(serialized, List.class);247 assertEquals(100, readObject.size());248 }249 @Test250 public void should_serialize_object_mock() throws Exception {251 // given252 Any mock = mock(Any.class);253 // when254 ByteArrayOutputStream serialized = serializeMock(mock);255 // then256 deserializeMock(serialized, Any.class);257 }258 @Test259 public void should_serialize_real_partial_mock() throws Exception {260 // given261 Any mock = mock(Any.class, withSettings().serializable());262 when(mock.matches(anyObject())).thenCallRealMethod();263 // when264 ByteArrayOutputStream serialized = serializeMock(mock);265 // then266 Any readObject = deserializeMock(serialized, Any.class);267 readObject.matches("");268 }269 class AlreadySerializable implements Serializable {}270 @Test271 public void should_serialize_already_serializable_class() throws Exception {272 // given273 AlreadySerializable mock = mock(AlreadySerializable.class, withSettings().serializable());274 when(mock.toString()).thenReturn("foo");275 // when276 mock = serializeAndBack(mock);277 // then278 assertEquals("foo", mock.toString());279 }280 @Test281 public void should_be_serialize_and_have_extra_interfaces() throws Exception {282 //when283 IMethods mock = mock(IMethods.class, withSettings().serializable().extraInterfaces(List.class));284 IMethods mockTwo = mock(IMethods.class, withSettings().extraInterfaces(List.class).serializable());285 //then286 Assertions.assertThat((Object) serializeAndBack((List) mock))287 .isInstanceOf(List.class)288 .isInstanceOf(IMethods.class);289 Assertions.assertThat((Object) serializeAndBack((List) mockTwo))290 .isInstanceOf(List.class)291 .isInstanceOf(IMethods.class);292 }293 static class NotSerializableAndNoDefaultConstructor {294 NotSerializableAndNoDefaultConstructor(Observable o) { super(); }295 }296 @Test297 public void should_fail_when_serializable_used_with_type_that_dont_implements_Serializable_and_dont_declare_a_no_arg_constructor() throws Exception {298 try {299 serializeAndBack(mock(NotSerializableAndNoDefaultConstructor.class, withSettings().serializable()));300 fail("should have thrown an exception to say the object is not serializable");301 } catch (MockitoException e) {302 Assertions.assertThat(e.getMessage())303 .contains(NotSerializableAndNoDefaultConstructor.class.getSimpleName())304 .contains("serializable()")305 .contains("implement Serializable")306 .contains("no-arg constructor");307 }308 }309 static class SerializableAndNoDefaultConstructor implements Serializable {310 SerializableAndNoDefaultConstructor(Observable o) { super(); }311 }312 @Test313 public void should_be_able_to_serialize_type_that_implements_Serializable_but_but_dont_declare_a_no_arg_constructor() throws Exception {314 serializeAndBack(mock(SerializableAndNoDefaultConstructor.class));315 }316 public static class AClassWithPrivateNoArgConstructor {317 private AClassWithPrivateNoArgConstructor() {}318 List returningSomething() { return Collections.emptyList(); }319 }320 @Test321 public void private_constructor_currently_not_supported_at_the_moment_at_deserialization_time() throws Exception {322 // given323 AClassWithPrivateNoArgConstructor mockWithPrivateConstructor = Mockito.mock(324 AClassWithPrivateNoArgConstructor.class,325 Mockito.withSettings().serializable()326 );327 try {328 // when329 SimpleSerializationUtil.serializeAndBack(mockWithPrivateConstructor);330 fail("should have thrown an ObjectStreamException or a subclass of it");331 } catch (ObjectStreamException e) {332 // then333 Assertions.assertThat(e.toString()).contains("no valid constructor");334 }335 }336 @Test337 public void BUG_ISSUE_399_try_some_mocks_with_current_answers() throws Exception {338 IMethods iMethods = mock(IMethods.class, withSettings().serializable().defaultAnswer(RETURNS_DEEP_STUBS));339 when(iMethods.iMethodsReturningMethod().linkedListReturningMethod().contains(anyString())).thenReturn(false);340 serializeAndBack(iMethods);341 }342}...

Full Screen

Full Screen

Source:MocksSerializationForAnnotationTest.java Github

copy

Full Screen

...35import static org.mockito.Mockito.verify;36import static org.mockito.Mockito.when;37import static org.mockito.Mockito.withSettings;38import static org.mockitoutil.SimpleSerializationUtil.deserializeMock;39import static org.mockitoutil.SimpleSerializationUtil.serializeAndBack;40import static org.mockitoutil.SimpleSerializationUtil.serializeMock;4142@SuppressWarnings({"unchecked", "serial"})43public class MocksSerializationForAnnotationTest extends TestBase implements Serializable {4445 private static final long serialVersionUID = 6160482220413048624L;4647 @Mock Any any;48 @Mock(serializable=true) Bar barMock;49 @Mock(serializable=true) IMethods imethodsMock;50 @Mock(serializable=true) IMethods imethodsMock2;51 @Mock(serializable=true) Any anyMock;52 @Mock(serializable=true) AlreadySerializable alreadySerializableMock;53 @Mock(extraInterfaces={List.class},serializable=true) IMethods imethodsWithExtraInterfacesMock;54 55 @Test56 public void should_allow_throws_exception_to_be_serializable() throws Exception {57 // given58 when(barMock.doSomething()).thenAnswer(new ThrowsException(new RuntimeException()));5960 //when-serialize then-deserialize61 serializeAndBack(barMock);62 }63 64 @Test65 public void should_allow_mock_to_be_serializable() throws Exception {66 // when-serialize then-deserialize67 serializeAndBack(imethodsMock);68 }6970 @Test71 public void should_allow_mock_and_boolean_value_to_serializable() throws Exception {72 // given73 when(imethodsMock.booleanReturningMethod()).thenReturn(true);7475 // when76 ByteArrayOutputStream serialized = serializeMock(imethodsMock);7778 // then79 IMethods readObject = deserializeMock(serialized, IMethods.class);80 assertTrue(readObject.booleanReturningMethod());81 }8283 @Test84 public void should_allow_mock_and_string_value_to_be_serializable() throws Exception {85 // given86 String value = "value";87 when(imethodsMock.stringReturningMethod()).thenReturn(value);8889 // when90 ByteArrayOutputStream serialized = serializeMock(imethodsMock);9192 // then93 IMethods readObject = deserializeMock(serialized, IMethods.class);94 assertEquals(value, readObject.stringReturningMethod());95 }9697 @Test98 public void should_all_mock_and_serializable_value_to_be_serialized() throws Exception {99 // given100 List<?> value = Collections.emptyList();101 when(imethodsMock.objectReturningMethodNoArgs()).thenReturn(value);102103 // when104 ByteArrayOutputStream serialized = serializeMock(imethodsMock);105106 // then107 IMethods readObject = deserializeMock(serialized, IMethods.class);108 assertEquals(value, readObject.objectReturningMethodNoArgs());109 }110111 @Test112 public void should_serialize_method_call_with_parameters_that_are_serializable() throws Exception {113 List<?> value = Collections.emptyList();114 when(imethodsMock.objectArgMethod(value)).thenReturn(value);115116 // when117 ByteArrayOutputStream serialized = serializeMock(imethodsMock);118119 // then120 IMethods readObject = deserializeMock(serialized, IMethods.class);121 assertEquals(value, readObject.objectArgMethod(value));122 }123124 @Test125 public void should_serialize_method_calls_using_any_string_matcher() throws Exception {126 List<?> value = Collections.emptyList();127 when(imethodsMock.objectArgMethod(anyString())).thenReturn(value);128129 // when130 ByteArrayOutputStream serialized = serializeMock(imethodsMock);131132 // then133 IMethods readObject = deserializeMock(serialized, IMethods.class);134 assertEquals(value, readObject.objectArgMethod(""));135 }136137 @Test138 public void should_verify_called_n_times_for_serialized_mock() throws Exception {139 List<?> value = Collections.emptyList();140 when(imethodsMock.objectArgMethod(anyString())).thenReturn(value);141 imethodsMock.objectArgMethod("");142143 // when144 ByteArrayOutputStream serialized = serializeMock(imethodsMock);145146 // then147 IMethods readObject = deserializeMock(serialized, IMethods.class);148 verify(readObject, times(1)).objectArgMethod("");149 }150151 @Test152 public void should_verify_even_if_some_methods_called_after_serialization() throws Exception {153154 // when155 imethodsMock.simpleMethod(1);156 ByteArrayOutputStream serialized = serializeMock(imethodsMock);157 IMethods readObject = deserializeMock(serialized, IMethods.class);158 readObject.simpleMethod(1);159160 // then161 verify(readObject, times(2)).simpleMethod(1);162163 //this test is working because it seems that java serialization mechanism replaces all instances164 //of serialized object in the object graph (if there are any)165 }166167 class Bar implements Serializable {168 Foo foo;169170 public Foo doSomething() {171 return foo;172 }173 }174175 class Foo implements Serializable {176 Bar bar;177 Foo() {178 bar = new Bar();179 bar.foo = this;180 }181 }182183 @Test184 public void should_serialization_work() throws Exception {185 //given186 Foo foo = new Foo();187 //when188 foo = serializeAndBack(foo);189 //then190 assertSame(foo, foo.bar.foo);191 }192193 @Test194 public void should_stub_even_if_some_methods_called_after_serialization() throws Exception {195 //given196 // when197 when(imethodsMock.simpleMethod(1)).thenReturn("foo");198 ByteArrayOutputStream serialized = serializeMock(imethodsMock);199 IMethods readObject = deserializeMock(serialized, IMethods.class);200 when(readObject.simpleMethod(2)).thenReturn("bar");201202 // then203 assertEquals("foo", readObject.simpleMethod(1));204 assertEquals("bar", readObject.simpleMethod(2));205 }206207 @Test208 public void should_verify_call_order_for_serialized_mock() throws Exception {209 imethodsMock.arrayReturningMethod();210 imethodsMock2.arrayReturningMethod();211212 // when213 ByteArrayOutputStream serialized = serializeMock(imethodsMock);214 ByteArrayOutputStream serialized2 = serializeMock(imethodsMock2);215216 // then217 IMethods readObject = deserializeMock(serialized, IMethods.class);218 IMethods readObject2 = deserializeMock(serialized2, IMethods.class);219 InOrder inOrder = inOrder(readObject, readObject2);220 inOrder.verify(readObject).arrayReturningMethod();221 inOrder.verify(readObject2).arrayReturningMethod();222 }223224 @Test225 public void should_remember_interactions_for_serialized_mock() throws Exception {226 List<?> value = Collections.emptyList();227 when(imethodsMock.objectArgMethod(anyString())).thenReturn(value);228 imethodsMock.objectArgMethod("happened");229230 // when231 ByteArrayOutputStream serialized = serializeMock(imethodsMock);232233 // then234 IMethods readObject = deserializeMock(serialized, IMethods.class);235 verify(readObject, never()).objectArgMethod("never happened");236 }237238 @Test239 public void should_serialize_with_stubbing_callback() throws Exception {240241 // given242 CustomAnswersMustImplementSerializableForSerializationToWork answer = 243 new CustomAnswersMustImplementSerializableForSerializationToWork();244 answer.string = "return value";245 when(imethodsMock.objectArgMethod(anyString())).thenAnswer(answer);246247 // when248 ByteArrayOutputStream serialized = serializeMock(imethodsMock);249250 // then251 IMethods readObject = deserializeMock(serialized, IMethods.class);252 assertEquals(answer.string, readObject.objectArgMethod(""));253 }254255 static class CustomAnswersMustImplementSerializableForSerializationToWork256 implements Answer<Object>, Serializable {257 private String string;258 public Object answer(InvocationOnMock invocation) throws Throwable {259 invocation.getArguments();260 invocation.getMock();261 return string;262 }263 }264265 @Test266 public void should_serialize_with_real_object_spy() throws Exception {267 // given268 List<Object> list = new ArrayList<Object>();269 List<Object> spy = mock(ArrayList.class, withSettings()270 .spiedInstance(list)271 .defaultAnswer(CALLS_REAL_METHODS)272 .serializable());273 when(spy.size()).thenReturn(100);274275 // when276 ByteArrayOutputStream serialized = serializeMock(spy);277278 // then279 List<?> readObject = deserializeMock(serialized, List.class);280 assertEquals(100, readObject.size());281 }282283 @Test284 public void should_serialize_object_mock() throws Exception {285 // when286 ByteArrayOutputStream serialized = serializeMock(any);287288 // then289 deserializeMock(serialized, Any.class);290 }291 292 @Test293 public void should_serialize_real_partial_mock() throws Exception {294 // given295 when(anyMock.matches(anyObject())).thenCallRealMethod();296297 // when298 ByteArrayOutputStream serialized = serializeMock(anyMock);299300 // then301 Any readObject = deserializeMock(serialized, Any.class);302 readObject.matches("");303 }304305 class AlreadySerializable implements Serializable {}306307 @Test308 public void should_serialize_already_serializable_class() throws Exception {309 // given310 when(alreadySerializableMock.toString()).thenReturn("foo");311312 // when313 alreadySerializableMock = serializeAndBack(alreadySerializableMock);314315 // then316 assertEquals("foo", alreadySerializableMock.toString());317 }318 319 @Test320 public void should_be_serialize_and_have_extra_interfaces() throws Exception {321 //then322 Assertions.assertThat((Object) serializeAndBack((List) imethodsWithExtraInterfacesMock))323 .isInstanceOf(List.class)324 .isInstanceOf(IMethods.class);325 }326327328329 static class NotSerializableAndNoDefaultConstructor {330 NotSerializableAndNoDefaultConstructor(Observable o) { super(); }331 }332 333 public static class FailTestClass {334 @Mock(serializable=true)335 NotSerializableAndNoDefaultConstructor notSerializableAndNoDefaultConstructor;336 }337 338 @Test339 public void should_fail_when_serializable_used_with_type_that_dont_implements_Serializable_and_dont_declare_a_no_arg_constructor() throws Exception {340 try {341 FailTestClass testClass = new FailTestClass();342 MockitoAnnotations.initMocks(testClass);343 serializeAndBack(testClass.notSerializableAndNoDefaultConstructor);344 fail("should have thrown an exception to say the object is not serializable");345 } catch (MockitoException e) {346 Assertions.assertThat(e.getMessage())347 .contains(NotSerializableAndNoDefaultConstructor.class.getSimpleName())348 .contains("serializable()")349 .contains("implement Serializable")350 .contains("no-arg constructor");351 }352 }353354355356 static class SerializableAndNoDefaultConstructor implements Serializable {357 SerializableAndNoDefaultConstructor(Observable o) { super(); }358 }359360 public static class TestClassThatHoldValidField {361 @Mock(serializable=true)362 SerializableAndNoDefaultConstructor serializableAndNoDefaultConstructor;363 }364365 @Test366 public void should_be_able_to_serialize_type_that_implements_Serializable_but_but_dont_declare_a_no_arg_constructor() throws Exception {367 TestClassThatHoldValidField testClass = new TestClassThatHoldValidField();368 MockitoAnnotations.initMocks(testClass);369370 serializeAndBack(testClass.serializableAndNoDefaultConstructor);371 } ...

Full Screen

Full Screen

Source:ObjectsSerializationTest.java Github

copy

Full Screen

...5package org.mockitousage.basicapi;6import org.junit.Test;7import org.mockitoutil.TestBase;8import java.io.Serializable;9import static org.mockitoutil.SimpleSerializationUtil.serializeAndBack;10@SuppressWarnings("serial")11public class ObjectsSerializationTest extends TestBase implements Serializable {12 //Ok, this test has nothing to do with mocks but it shows fundamental feature of java serialization that13 //plays important role in mocking:14 //Serialization/deserialization actually replaces all instances of serialized object in the object graph (if there are any)15 //thanks to that mechanizm, stubbing & verification can correctly match method invocations because16 //one of the parts of invocation matching is checking if mock object is the same17 class Bar implements Serializable {18 Foo foo;19 }20 class Foo implements Serializable {21 Bar bar;22 Foo() {23 bar = new Bar();24 bar.foo = this;25 }26 }27 @Test28 public void shouldSerializationWork() throws Exception {29 //given30 Foo foo = new Foo();31 //when32 foo = serializeAndBack(foo);33 //then34 assertSame(foo, foo.bar.foo);35 }36}...

Full Screen

Full Screen

serializeAndBack

Using AI Code Generation

copy

Full Screen

1package org.mockitoutil;2import java.io.*;3public class SimpleSerializationUtil {4 public static <T> T serializeAndBack(T object) {5 try {6 ByteArrayOutputStream baos = new ByteArrayOutputStream();7 ObjectOutputStream oos = new ObjectOutputStream(baos);8 oos.writeObject(object);9 oos.close();10 byte[] bytes = baos.toByteArray();11 ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes));12 return (T) ois.readObject();13 } catch (IOException e) {14 throw new RuntimeException(e);15 } catch (ClassNotFoundException e) {16 throw new RuntimeException(e);17 }18 }19}20package org.mockitoutil;21import org.junit.Test;22import java.io.Serializable;23import static org.junit.Assert.assertEquals;24public class SimpleSerializationUtilTest {25 public void shouldSerializeAndDeserialize() throws Exception {26 SomeObject object = new SomeObject();27 object = serializeAndBack(object);28 assertEquals("value", object.value);29 }30 public static class SomeObject implements Serializable {31 public String value = "value";32 }33}

Full Screen

Full Screen

serializeAndBack

Using AI Code Generation

copy

Full Screen

1package org.mockitoutil;2import java.io.*;3public class SimpleSerializationUtil {4 public static Object serializeAndBack(Serializable s) throws Exception {5 ByteArrayOutputStream baos = new ByteArrayOutputStream();6 ObjectOutputStream oos = new ObjectOutputStream(baos);7 oos.writeObject(s);8 oos.flush();9 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());10 ObjectInputStream ois = new ObjectInputStream(bais);11 return ois.readObject();12 }13}14package org.mockitoutil;15import java.io.*;16import java.util.*;17public class SimpleSerializationUtil {18 public static Object serializeAndBack(Serializable s) throws Exception {19 ByteArrayOutputStream baos = new ByteArrayOutputStream();20 ObjectOutputStream oos = new ObjectOutputStream(baos);21 oos.writeObject(s);22 oos.flush();23 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());24 ObjectInputStream ois = new ObjectInputStream(bais);25 return ois.readObject();26 }27}28package org.mockitoutil;29import java.io.*;30import java.util.*;31public class SimpleSerializationUtil {32 public static Object serializeAndBack(Serializable s) throws Exception {33 ByteArrayOutputStream baos = new ByteArrayOutputStream();34 ObjectOutputStream oos = new ObjectOutputStream(baos);35 oos.writeObject(s);36 oos.flush();37 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());38 ObjectInputStream ois = new ObjectInputStream(bais);39 return ois.readObject();40 }41}42package org.mockitoutil;43import java.io.*;44import java.util.*;45public class SimpleSerializationUtil {46 public static Object serializeAndBack(Serializable s) throws Exception {47 ByteArrayOutputStream baos = new ByteArrayOutputStream();48 ObjectOutputStream oos = new ObjectOutputStream(baos);49 oos.writeObject(s);50 oos.flush();51 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());52 ObjectInputStream ois = new ObjectInputStream(bais);53 return ois.readObject();54 }55}56package org.mockitoutil;57import java.io.*;58import java.util.*;

Full Screen

Full Screen

serializeAndBack

Using AI Code Generation

copy

Full Screen

1package org.mockitoutil;2import java.io.*;3public class SimpleSerializationUtil {4 public static Object serializeAndBack(Object original) throws IOException, ClassNotFoundException {5 ByteArrayOutputStream baos = new ByteArrayOutputStream();6 ObjectOutputStream oos = new ObjectOutputStream(baos);7 oos.writeObject(original);8 oos.close();9 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());10 ObjectInputStream ois = new ObjectInputStream(bais);11 return ois.readObject();12 }13}14package org.mockitoutil;15import java.io.*;16public class SimpleSerializationUtil {17 public static Object serializeAndBack(Object original) throws IOException, ClassNotFoundException {18 ByteArrayOutputStream baos = new ByteArrayOutputStream();19 ObjectOutputStream oos = new ObjectOutputStream(baos);20 oos.writeObject(original);21 oos.close();22 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());23 ObjectInputStream ois = new ObjectInputStream(bais);24 return ois.readObject();25 }26}27package org.mockitoutil;28import java.io.*;29public class SimpleSerializationUtil {30 public static Object serializeAndBack(Object original) throws IOException, ClassNotFoundException {31 ByteArrayOutputStream baos = new ByteArrayOutputStream();32 ObjectOutputStream oos = new ObjectOutputStream(baos);33 oos.writeObject(original);34 oos.close();35 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());36 ObjectInputStream ois = new ObjectInputStream(bais);37 return ois.readObject();38 }39}40package org.mockitoutil;41import java.io.*;42public class SimpleSerializationUtil {43 public static Object serializeAndBack(Object original) throws IOException, ClassNotFoundException {44 ByteArrayOutputStream baos = new ByteArrayOutputStream();45 ObjectOutputStream oos = new ObjectOutputStream(baos);46 oos.writeObject(original);47 oos.close();48 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());49 ObjectInputStream ois = new ObjectInputStream(bais);50 return ois.readObject();51 }52}

Full Screen

Full Screen

serializeAndBack

Using AI Code Generation

copy

Full Screen

1import org.junit.Test;2import org.mockitoutil.SimpleSerializationUtil;3import static org.junit.Assert.assertEquals;4import static org.junit.Assert.assertTrue;5public class SerializeAndDeserializeTest {6 public void serializeAndDeserializeTest() {7 String str = "Hello World";8 String str1 = SimpleSerializationUtil.serializeAndBack(str);9 assertEquals(str, str1);10 }11}12import org.junit.Test;13import org.mockitoutil.SimpleSerializationUtil;14import static org.junit.Assert.assertEquals;15import static org.junit.Assert.assertTrue;16public class SerializeAndDeserializeTest {17 public void serializeAndDeserializeTest() {18 String str = "Hello World";19 String str1 = SimpleSerializationUtil.serializeAndBack(str);20 assertEquals(str, str1);21 }22}23import org.junit.Test;24import org.mockitoutil.SimpleSerializationUtil;25import static org.junit.Assert.assertEquals;26import static org.junit.Assert.assertTrue;27public class SerializeAndDeserializeTest {28 public void serializeAndDeserializeTest() {29 String str = "Hello World";30 String str1 = SimpleSerializationUtil.serializeAndBack(str);31 assertEquals(str, str1);32 }33}34import org.junit.Test;35import org.mockitoutil.SimpleSerializationUtil;36import static org.junit.Assert.assertEquals;37import static org.junit.Assert.assertTrue;38public class SerializeAndDeserializeTest {39 public void serializeAndDeserializeTest() {40 String str = "Hello World";41 String str1 = SimpleSerializationUtil.serializeAndBack(str);

Full Screen

Full Screen

serializeAndBack

Using AI Code Generation

copy

Full Screen

1import org.junit.Test;2import org.mockitoutil.SimpleSerializationUtil;3import static org.junit.Assert.*;4public class SerializeAndBackTest {5 public void test() throws Exception {6 String str = "test";7 String serialized = SimpleSerializationUtil.serializeAndBack(str);8 assertEquals(str, serialized);9 }10}

Full Screen

Full Screen

serializeAndBack

Using AI Code Generation

copy

Full Screen

1package org.mockitoutil;2import java.io.*;3import java.util.*;4public class SimpleSerializationUtil {5 public static <T> T serializeAndBack(T toBeSerialized) {6 try {7 ByteArrayOutputStream baos = new ByteArrayOutputStream();8 ObjectOutputStream oos = new ObjectOutputStream(baos);9 oos.writeObject(toBeSerialized);10 oos.close();11 byte[] bytes = baos.toByteArray();12 ByteArrayInputStream bais = new ByteArrayInputStream(bytes);13 ObjectInputStream ois = new ObjectInputStream(bais);14 return (T) ois.readObject();15 } catch (Exception e) {16 e.printStackTrace();17 throw new RuntimeException(e);18 }19 }20}21package org.mockitoutil;22import java.io.*;23import java.util.*;24public class SimpleSerializationUtil {25 public static <T> T serializeAndBack(T toBeSerialized) {26 try {27 ByteArrayOutputStream baos = new ByteArrayOutputStream();28 ObjectOutputStream oos = new ObjectOutputStream(baos);29 oos.writeObject(toBeSerialized);30 oos.close();31 byte[] bytes = baos.toByteArray();32 ByteArrayInputStream bais = new ByteArrayInputStream(bytes);33 ObjectInputStream ois = new ObjectInputStream(bais);34 return (T) ois.readObject();35 } catch (Exception e) {36 e.printStackTrace();37 throw new RuntimeException(e);38 }39 }40}41package org.mockitoutil;42import java.io.*;43import java.util.*;44public class SimpleSerializationUtil {45 public static <T> T serializeAndBack(T toBeSerialized) {46 try {47 ByteArrayOutputStream baos = new ByteArrayOutputStream();48 ObjectOutputStream oos = new ObjectOutputStream(baos);49 oos.writeObject(toBeSerialized);50 oos.close();51 byte[] bytes = baos.toByteArray();52 ByteArrayInputStream bais = new ByteArrayInputStream(bytes);53 ObjectInputStream ois = new ObjectInputStream(bais);54 return (T) ois.readObject();55 } catch (Exception e) {56 e.printStackTrace();57 throw new RuntimeException(e);58 }59 }60}

Full Screen

Full Screen

serializeAndBack

Using AI Code Generation

copy

Full Screen

1package org.mockito.internal.creation.bytebuddy;2import org.junit.Test;3import org.junit.runner.RunWith;4import org.junit.runners.Parameterized;5import org.mockito.internal.creation.bytebuddy.MockMethodInterceptor.MockAccess;6import org.mockitoutil.SimpleSerializationUtil;7import java.io.Serializable;8import java.util.Arrays;9import java.util.Collection;10import static org.mockito.Mockito.mock;11@RunWith(Parameterized.class)12public class MockMethodInterceptorMockAccessSerializationTest {13 private final MockAccess mockAccess;14 public MockMethodInterceptorMockAccessSerializationTest(MockAccess mockAccess) {15 this.mockAccess = mockAccess;16 }17 public static Collection<Object[]> data() {18 return Arrays.asList(new Object[][]{19 {MockAccess.of(mock(Serializable.class))}20 });21 }22 public void testMockAccessSerialization() throws Exception {23 SimpleSerializationUtil.serializeAndBack(mockAccess);24 }25}26 at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)27 at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)28 at org.mockitoutil.SimpleSerializationUtil.serializeAndBack(SimpleSerializationUtil.java:30)29 at org.mockito.internal.creation.bytebuddy.MockMethodInterceptorMockAccessSerializationTest.testMockAccessSerialization(MockMethodInterceptorMockAccessSerializationTest.java:33)30SimpleSerializationUtil.serializeAndBack(mockAccess, MockAccess.class);31ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));32SimpleSerializationUtil.serializeAndBack(mockAccess, MockAccess.class, TypeDescription.ForLoadedType.class);33ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()), new ClassLoaderObject

Full Screen

Full Screen

serializeAndBack

Using AI Code Generation

copy

Full Screen

1import org.mockitoutil.SimpleSerializationUtil;2import org.mockito.internal.util.MockUtil;3import org.mockito.internal.creation.bytebuddy.MockMethodInterceptor;4import org.mockito.internal.creation.bytebuddy.MockAccess;5import org.mockito.internal.creation.bytebuddy.MockMethodAdvice;6import org.mockito.internal.creation.bytebuddy.CachingMockBytecodeGenerator;7import org.mockito.internal.creation.bytebuddy.MockFeatures;8import org.mockito.internal.creation.bytebuddy.MockMethodDispatcher;9import org.mockito.internal.creation.bytebuddy.MockMethodAdvice.ArgumentProvider;10import org.mockito.internal.creation.bytebuddy.MockMethodAdvice.DispatcherProvider;11import org.mockito.internal.creation.bytebuddy.MockMethodAdvice.ReturnValueProvider;12import org.mockito.internal.creation.bytebuddy.MockMethodAdvice.ExceptionHandler;13import org.mockito.internal.creation.bytebuddy.MockMethodAdvice.ReturnValueValidator;14import org.mockito.internal.creation.bytebuddy.MockMethodAdvice.ExceptionValidator;15import org.mockito.internal.creation.bytebuddy.MockMethodAdvice.WithExtraState;16import org.mockito.internal.creation.bytebuddy.MockMethodAdvice.WithGenericSignature;17import org.mockito.internal.creation.bytebuddy.MockMethodAdvice.WithGenericSignatureAndExtraState;18import org.mockito.internal.creation.bytebuddy.MockMethodAdvice.WithGenericSignatureAndExtraStateAndArgumentProvider;19import org.mockito.internal.creation.bytebuddy.MockMethodAdvice.WithGenericSignatureAndArgumentProvider;20import org.mockito.internal.creation.bytebuddy.MockMethodAdvice.WithGenericSignatureAndDispatcherProvider;21import org.mockito.internal.creation.bytebuddy.MockMethodAdvice.WithGenericSignatureAndExtraStateAndDispatcherProvider;22import org.mockito.internal.creation.bytebuddy.MockMethodAdvice.WithGenericSignatureAndExtraStateAndDispatcherProviderAndArgumentProvider;23import org.mockito.internal.creation.bytebuddy.MockMethodAdvice.WithGenericSignatureAndExtraStateAndDispatcherProviderAndArgumentProviderAndReturnValueProvider;24import org.mockito.internal.creation.bytebuddy.MockMethodAdvice.WithGenericSignatureAndExtraStateAndDispatcherProviderAndArgumentProviderAndReturnValueProviderAndExceptionHandler;25import org.mockito.internal.creation.bytebuddy.MockMethodAdvice.WithGenericSignatureAndExtraStateAndDispatcherProviderAndArgumentProviderAndReturnValueProviderAndExceptionHandlerAndReturnValueValidator;26import org.mockito.internal.creation.bytebuddy.MockMethodAdvice.WithGenericSignatureAndExtraStateAndDispatcherProviderAndArgumentProviderAndReturnValueProviderAndExceptionHandlerAndReturnValueValidatorAndExceptionValidator;27import org.mockito.internal.creation.bytebuddy.MockMethodAdvice.WithGenericSignatureAndDispatcherProviderAndArgumentProvider;28import org.mockito.internal.creation.bytebuddy.MockMethod

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Mockito automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in SimpleSerializationUtil

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful