1 /* 2 * Copyright (c) 2007 Mockito contributors 3 * This program is made available under the terms of the MIT License. 4 */ 5 6 package org.mockitousage.basicapi; 7 8 import org.assertj.core.api.Assertions; 9 import org.junit.Test; 10 import org.mockito.InOrder; 11 import org.mockito.Mock; 12 import org.mockito.MockitoAnnotations; 13 import org.mockito.internal.matchers.Any; 14 import org.mockito.internal.stubbing.answers.ThrowsException; 15 import org.mockito.invocation.InvocationOnMock; 16 import org.mockito.stubbing.Answer; 17 import org.mockitousage.IMethods; 18 import org.mockitoutil.TestBase; 19 20 import java.io.ByteArrayOutputStream; 21 import java.io.Serializable; 22 import java.util.Collections; 23 import java.util.List; 24 import java.util.Observable; 25 26 import static org.junit.Assert.*; 27 import static org.mockito.Mockito.*; 28 import static org.mockitoutil.SimpleSerializationUtil.*; 29 30 @SuppressWarnings({"unchecked", "serial"}) 31 public class MocksSerializationForAnnotationTest extends TestBase implements Serializable { 32 33 private static final long serialVersionUID = 6160482220413048624L; 34 35 @Mock Any any; 36 @Mock(serializable=true) Bar barMock; 37 @Mock(serializable=true) IMethods imethodsMock; 38 @Mock(serializable=true) IMethods imethodsMock2; 39 @Mock(serializable=true) Any anyMock; 40 @Mock(serializable=true) AlreadySerializable alreadySerializableMock; 41 @Mock(extraInterfaces={List.class},serializable=true) IMethods imethodsWithExtraInterfacesMock; 42 43 @Test 44 public void should_allow_throws_exception_to_be_serializable() throws Exception { 45 // given 46 when(barMock.doSomething()).thenAnswer(new ThrowsException(new RuntimeException())); 47 48 //when-serialize then-deserialize 49 serializeAndBack(barMock); 50 } 51 52 @Test 53 public void should_allow_mock_to_be_serializable() throws Exception { 54 // when-serialize then-deserialize 55 serializeAndBack(imethodsMock); 56 } 57 58 @Test 59 public void should_allow_mock_and_boolean_value_to_serializable() throws Exception { 60 // given 61 when(imethodsMock.booleanReturningMethod()).thenReturn(true); 62 63 // when 64 ByteArrayOutputStream serialized = serializeMock(imethodsMock); 65 66 // then 67 IMethods readObject = deserializeMock(serialized, IMethods.class); 68 assertTrue(readObject.booleanReturningMethod()); 69 } 70 71 @Test 72 public void should_allow_mock_and_string_value_to_be_serializable() throws Exception { 73 // given 74 String value = "value"; 75 when(imethodsMock.stringReturningMethod()).thenReturn(value); 76 77 // when 78 ByteArrayOutputStream serialized = serializeMock(imethodsMock); 79 80 // then 81 IMethods readObject = deserializeMock(serialized, IMethods.class); 82 assertEquals(value, readObject.stringReturningMethod()); 83 } 84 85 @Test 86 public void should_all_mock_and_serializable_value_to_be_serialized() throws Exception { 87 // given 88 List<?> value = Collections.emptyList(); 89 when(imethodsMock.objectReturningMethodNoArgs()).thenReturn(value); 90 91 // when 92 ByteArrayOutputStream serialized = serializeMock(imethodsMock); 93 94 // then 95 IMethods readObject = deserializeMock(serialized, IMethods.class); 96 assertEquals(value, readObject.objectReturningMethodNoArgs()); 97 } 98 99 @Test 100 public void should_serialize_method_call_with_parameters_that_are_serializable() throws Exception { 101 List<?> value = Collections.emptyList(); 102 when(imethodsMock.objectArgMethod(value)).thenReturn(value); 103 104 // when 105 ByteArrayOutputStream serialized = serializeMock(imethodsMock); 106 107 // then 108 IMethods readObject = deserializeMock(serialized, IMethods.class); 109 assertEquals(value, readObject.objectArgMethod(value)); 110 } 111 112 @Test 113 public void should_serialize_method_calls_using_any_string_matcher() throws Exception { 114 List<?> value = Collections.emptyList(); 115 when(imethodsMock.objectArgMethod(anyString())).thenReturn(value); 116 117 // when 118 ByteArrayOutputStream serialized = serializeMock(imethodsMock); 119 120 // then 121 IMethods readObject = deserializeMock(serialized, IMethods.class); 122 assertEquals(value, readObject.objectArgMethod("")); 123 } 124 125 @Test 126 public void should_verify_called_n_times_for_serialized_mock() throws Exception { 127 List<?> value = Collections.emptyList(); 128 when(imethodsMock.objectArgMethod(anyString())).thenReturn(value); 129 imethodsMock.objectArgMethod(""); 130 131 // when 132 ByteArrayOutputStream serialized = serializeMock(imethodsMock); 133 134 // then 135 IMethods readObject = deserializeMock(serialized, IMethods.class); 136 verify(readObject, times(1)).objectArgMethod(""); 137 } 138 139 @Test 140 public void should_verify_even_if_some_methods_called_after_serialization() throws Exception { 141 142 // when 143 imethodsMock.simpleMethod(1); 144 ByteArrayOutputStream serialized = serializeMock(imethodsMock); 145 IMethods readObject = deserializeMock(serialized, IMethods.class); 146 readObject.simpleMethod(1); 147 148 // then 149 verify(readObject, times(2)).simpleMethod(1); 150 151 //this test is working because it seems that java serialization mechanism replaces all instances 152 //of serialized object in the object graph (if there are any) 153 } 154 155 class Bar implements Serializable { 156 Foo foo; 157 158 public Foo doSomething() { 159 return foo; 160 } 161 } 162 163 class Foo implements Serializable { 164 Bar bar; 165 Foo() { 166 bar = new Bar(); 167 bar.foo = this; 168 } 169 } 170 171 @Test 172 public void should_serialization_work() throws Exception { 173 //given 174 Foo foo = new Foo(); 175 //when 176 foo = serializeAndBack(foo); 177 //then 178 assertSame(foo, foo.bar.foo); 179 } 180 181 @Test 182 public void should_stub_even_if_some_methods_called_after_serialization() throws Exception { 183 //given 184 // when 185 when(imethodsMock.simpleMethod(1)).thenReturn("foo"); 186 ByteArrayOutputStream serialized = serializeMock(imethodsMock); 187 IMethods readObject = deserializeMock(serialized, IMethods.class); 188 when(readObject.simpleMethod(2)).thenReturn("bar"); 189 190 // then 191 assertEquals("foo", readObject.simpleMethod(1)); 192 assertEquals("bar", readObject.simpleMethod(2)); 193 } 194 195 @Test 196 public void should_verify_call_order_for_serialized_mock() throws Exception { 197 imethodsMock.arrayReturningMethod(); 198 imethodsMock2.arrayReturningMethod(); 199 200 // when 201 ByteArrayOutputStream serialized = serializeMock(imethodsMock); 202 ByteArrayOutputStream serialized2 = serializeMock(imethodsMock2); 203 204 // then 205 IMethods readObject = deserializeMock(serialized, IMethods.class); 206 IMethods readObject2 = deserializeMock(serialized2, IMethods.class); 207 InOrder inOrder = inOrder(readObject, readObject2); 208 inOrder.verify(readObject).arrayReturningMethod(); 209 inOrder.verify(readObject2).arrayReturningMethod(); 210 } 211 212 @Test 213 public void should_remember_interactions_for_serialized_mock() throws Exception { 214 List<?> value = Collections.emptyList(); 215 when(imethodsMock.objectArgMethod(anyString())).thenReturn(value); 216 imethodsMock.objectArgMethod("happened"); 217 218 // when 219 ByteArrayOutputStream serialized = serializeMock(imethodsMock); 220 221 // then 222 IMethods readObject = deserializeMock(serialized, IMethods.class); 223 verify(readObject, never()).objectArgMethod("never happened"); 224 } 225 226 @Test 227 public void should_serialize_with_stubbing_callback() throws Exception { 228 229 // given 230 CustomAnswersMustImplementSerializableForSerializationToWork answer = 231 new CustomAnswersMustImplementSerializableForSerializationToWork(); 232 answer.string = "return value"; 233 when(imethodsMock.objectArgMethod(anyString())).thenAnswer(answer); 234 235 // when 236 ByteArrayOutputStream serialized = serializeMock(imethodsMock); 237 238 // then 239 IMethods readObject = deserializeMock(serialized, IMethods.class); 240 assertEquals(answer.string, readObject.objectArgMethod("")); 241 } 242 243 static class CustomAnswersMustImplementSerializableForSerializationToWork 244 implements Answer<Object>, Serializable { 245 private String string; 246 public Object answer(InvocationOnMock invocation) throws Throwable { 247 invocation.getArguments(); 248 invocation.getMock(); 249 return string; 250 } 251 } 252 253 @Test 254 public void should_serialize_with_real_object_spy() throws Exception { 255 // given 256 SerializableSample list = new SerializableSample(); 257 SerializableSample spy = mock(SerializableSample.class, withSettings() 258 .spiedInstance(list) 259 .defaultAnswer(CALLS_REAL_METHODS) 260 .serializable()); 261 when(spy.foo()).thenReturn("foo"); 262 263 // when 264 ByteArrayOutputStream serialized = serializeMock(spy); 265 266 // then 267 SerializableSample readObject = deserializeMock(serialized, SerializableSample.class); 268 assertEquals("foo", readObject.foo()); 269 } 270 271 @Test 272 public void should_serialize_object_mock() throws Exception { 273 // when 274 ByteArrayOutputStream serialized = serializeMock(any); 275 276 // then 277 deserializeMock(serialized, Any.class); 278 } 279 280 @Test 281 public void should_serialize_real_partial_mock() throws Exception { 282 // given 283 when(anyMock.matches(anyObject())).thenCallRealMethod(); 284 285 // when 286 ByteArrayOutputStream serialized = serializeMock(anyMock); 287 288 // then 289 Any readObject = deserializeMock(serialized, Any.class); 290 readObject.matches(""); 291 } 292 293 class AlreadySerializable implements Serializable {} 294 295 @Test 296 public void should_serialize_already_serializable_class() throws Exception { 297 // given 298 when(alreadySerializableMock.toString()).thenReturn("foo"); 299 300 // when 301 alreadySerializableMock = serializeAndBack(alreadySerializableMock); 302 303 // then 304 assertEquals("foo", alreadySerializableMock.toString()); 305 } 306 307 @Test 308 public void should_be_serialize_and_have_extra_interfaces() throws Exception { 309 //then 310 Assertions.assertThat((Object) serializeAndBack((List) imethodsWithExtraInterfacesMock)) 311 .isInstanceOf(List.class) 312 .isInstanceOf(IMethods.class); 313 } 314 315 static class NotSerializableAndNoDefaultConstructor { 316 NotSerializableAndNoDefaultConstructor(Observable o) { super(); } 317 } 318 319 static class SerializableAndNoDefaultConstructor implements Serializable { 320 SerializableAndNoDefaultConstructor(Observable o) { super(); } 321 } 322 323 public static class TestClassThatHoldValidField { 324 @Mock(serializable=true) 325 SerializableAndNoDefaultConstructor serializableAndNoDefaultConstructor; 326 } 327 328 @Test 329 public void should_be_able_to_serialize_type_that_implements_Serializable_but_but_dont_declare_a_no_arg_constructor() throws Exception { 330 TestClassThatHoldValidField testClass = new TestClassThatHoldValidField(); 331 MockitoAnnotations.initMocks(testClass); 332 333 serializeAndBack(testClass.serializableAndNoDefaultConstructor); 334 } 335 336 public static class SerializableSample implements Serializable { 337 338 public String foo() { 339 return null; 340 } 341 } 342 } 343