1/* 2 * Copyright (c) 2007 Mockito contributors 3 * This program is made available under the terms of the MIT License. 4 */ 5package org.mockito.internal.invocation; 6 7import org.junit.Before; 8import org.junit.Test; 9import org.mockitoutil.TestBase; 10 11import java.io.ByteArrayOutputStream; 12import java.io.ObjectOutputStream; 13import java.lang.reflect.Method; 14 15import static org.junit.Assert.*; 16 17 18public class SerializableMethodTest extends TestBase { 19 20 private MockitoMethod method; 21 private Method toStringMethod; 22 private Class<?>[] args; 23 24 @Before 25 public void createMethodToTestWith() throws SecurityException, NoSuchMethodException { 26 args = new Class<?>[0]; 27 toStringMethod = this.getClass().getMethod("toString", args); 28 method = new SerializableMethod(toStringMethod); 29 } 30 31 @Test 32 public void shouldBeSerializable() throws Exception { 33 ByteArrayOutputStream serialized = new ByteArrayOutputStream(); 34 new ObjectOutputStream(serialized).writeObject(method); 35 } 36 37 @Test 38 public void shouldBeAbleToRetrieveMethodExceptionTypes() throws Exception { 39 assertArrayEquals(toStringMethod.getExceptionTypes(), method.getExceptionTypes()); 40 } 41 42 @Test 43 public void shouldBeAbleToRetrieveMethodName() throws Exception { 44 assertEquals(toStringMethod.getName(), method.getName()); 45 } 46 47 @Test 48 public void shouldBeAbleToCheckIsArgVargs() throws Exception { 49 assertEquals(toStringMethod.isVarArgs(), method.isVarArgs()); 50 } 51 52 @Test 53 public void shouldBeAbleToGetParameterTypes() throws Exception { 54 assertArrayEquals(toStringMethod.getParameterTypes(), method.getParameterTypes()); 55 } 56 57 @Test 58 public void shouldBeAbleToGetReturnType() throws Exception { 59 assertEquals(toStringMethod.getReturnType(), method.getReturnType()); 60 } 61 62 @Test 63 public void shouldBeEqualForTwoInstances() throws Exception { 64 assertTrue(new SerializableMethod(toStringMethod).equals(method)); 65 } 66 67 @Test 68 public void shouldNotBeEqualForSameMethodFromTwoDifferentClasses() throws Exception { 69 Method testBaseToStringMethod = String.class.getMethod("toString", args); 70 assertFalse(new SerializableMethod(testBaseToStringMethod).equals(method)); 71 } 72 73 //TODO: add tests for generated equals() method 74 75} 76