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