Home | History | Annotate | Download | only in serialization
      1 package org.mockitousage.serialization;
      2 
      3 import org.junit.Test;
      4 
      5 import java.io.Serializable;
      6 import java.util.Iterator;
      7 import java.util.List;
      8 
      9 import static org.assertj.core.api.Assertions.assertThat;
     10 import static org.mockito.Mockito.*;
     11 import static org.mockitoutil.SimpleSerializationUtil.serializeAndBack;
     12 
     13 public class DeepStubsSerializableTest {
     14 
     15     @Test
     16     public void should_serialize_and_deserialize_mock_created_with_deep_stubs() throws Exception {
     17         // given
     18         SampleClass sampleClass = mock(SampleClass.class, withSettings().defaultAnswer(RETURNS_DEEP_STUBS).serializable());
     19         when(sampleClass.getSample().isFalse()).thenReturn(true);
     20         when(sampleClass.getSample().number()).thenReturn(999);
     21 
     22         // when
     23         SampleClass deserializedSample = serializeAndBack(sampleClass);
     24 
     25         // then
     26         assertThat(deserializedSample.getSample().isFalse()).isEqualTo(true);
     27         assertThat(deserializedSample.getSample().number()).isEqualTo(999);
     28     }
     29 
     30     @Test
     31     public void should_serialize_and_deserialize_parameterized_class_mocked_with_deep_stubs() throws Exception {
     32         // given
     33         ListContainer deep_stubbed = mock(ListContainer.class, withSettings().defaultAnswer(RETURNS_DEEP_STUBS).serializable());
     34         when(deep_stubbed.iterator().next().add("yes")).thenReturn(true);
     35 
     36         // when
     37         ListContainer deserialized_deep_stub = serializeAndBack(deep_stubbed);
     38 
     39         // then
     40         assertThat(deserialized_deep_stub.iterator().next().add("not stubbed but mock already previously resolved")).isEqualTo(false);
     41         assertThat(deserialized_deep_stub.iterator().next().add("yes")).isEqualTo(true);
     42     }
     43 
     44     @Test(expected = ClassCastException.class)
     45     public void should_discard_generics_metadata_when_serialized_then_disabling_deep_stubs_with_generics() throws Exception {
     46         // given
     47         ListContainer deep_stubbed = mock(ListContainer.class, withSettings().defaultAnswer(RETURNS_DEEP_STUBS).serializable());
     48         when(deep_stubbed.iterator().hasNext()).thenReturn(true);
     49 
     50         ListContainer deserialized_deep_stub = serializeAndBack(deep_stubbed);
     51 
     52         // when stubbing on a deserialized mock
     53         when(deserialized_deep_stub.iterator().next().get(42)).thenReturn("no");
     54 
     55         // then revert to the default RETURNS_DEEP_STUBS and the code will raise a ClassCastException
     56     }
     57 
     58 
     59     static class SampleClass implements Serializable {
     60         SampleClass2 getSample() { return new SampleClass2(); }
     61     }
     62 
     63     static class SampleClass2 implements Serializable {
     64         boolean isFalse() { return false; }
     65         int number() { return 100; }
     66     }
     67 
     68     static class Container<E> implements Iterable<E>, Serializable {
     69         private E e;
     70         public Container(E e) { this.e = e; }
     71         public E get() { return e; }
     72 
     73         public Iterator<E> iterator() {
     74             return new Iterator<E>() {
     75                 public boolean hasNext() { return true; }
     76                 public E next() { return e; }
     77                 public void remove() { }
     78             };
     79         }
     80     }
     81 
     82     static class ListContainer extends Container<List<String>> {
     83         public ListContainer(List<String> list) { super(list); }
     84     }
     85 }
     86