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