• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2007 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 package org.mockitousage.basicapi;
6 
7 import static org.junit.Assert.assertSame;
8 import static org.mockitoutil.SimpleSerializationUtil.serializeAndBack;
9 
10 import java.io.Serializable;
11 
12 import org.junit.Test;
13 import org.mockitoutil.TestBase;
14 
15 @SuppressWarnings("serial")
16 public class ObjectsSerializationTest extends TestBase implements Serializable {
17 
18     // Ok, this test has nothing to do with mocks but it shows fundamental feature of java
19     // serialization that
20     // plays important role in mocking:
21     // Serialization/deserialization actually replaces all instances of serialized object in the
22     // object graph (if there are any)
23     // thanks to that mechanizm, stubbing & verification can correctly match method invocations
24     // because
25     // one of the parts of invocation matching is checking if mock object is the same
26 
27     class Bar implements Serializable {
28         Foo foo;
29     }
30 
31     class Foo implements Serializable {
32         Bar bar;
33 
Foo()34         Foo() {
35             bar = new Bar();
36             bar.foo = this;
37         }
38     }
39 
40     @Test
shouldSerializationWork()41     public void shouldSerializationWork() throws Exception {
42         // given
43         Foo foo = new Foo();
44         // when
45         foo = serializeAndBack(foo);
46         // then
47         assertSame(foo, foo.bar.foo);
48     }
49 }
50