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 should_serialize_and_deserialize_mock_created_with_deep_stubs()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 should_serialize_and_deserialize_parameterized_class_mocked_with_deep_stubs()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) should_discard_generics_metadata_when_serialized_then_disabling_deep_stubs_with_generics()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 static class SampleClass implements Serializable { 63 getSample()64 SampleClass2 getSample() { 65 return new SampleClass2(); 66 } 67 } 68 69 static class SampleClass2 implements Serializable { 70 isFalse()71 boolean isFalse() { 72 return false; 73 } 74 number()75 int number() { 76 return 100; 77 } 78 } 79 80 static class Container<E> implements Iterable<E>, Serializable { 81 82 private E e; 83 Container(E e)84 public Container(E e) { 85 this.e = e; 86 } 87 get()88 public E get() { 89 return e; 90 } 91 iterator()92 public Iterator<E> iterator() { 93 return new Iterator<E>() { 94 public boolean hasNext() { 95 return true; 96 } 97 98 public E next() { 99 return e; 100 } 101 102 public void remove() { 103 } 104 }; 105 } 106 } 107 108 static class ListContainer extends Container<List<String>> { 109 110 public ListContainer(List<String> list) { 111 super(list); 112 } 113 } 114 } 115