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.mockito.internal.invocation; 7 8 import org.assertj.core.api.Assertions; 9 import org.junit.Before; 10 import org.junit.Test; 11 import org.mockito.exceptions.base.MockitoException; 12 import org.mockito.internal.invocation.realmethod.RealMethod; 13 import org.mockito.internal.matchers.ArrayEquals; 14 import org.mockito.internal.matchers.Equals; 15 import org.mockito.invocation.Invocation; 16 import org.mockitousage.IMethods; 17 import org.mockitoutil.TestBase; 18 19 import java.lang.reflect.Method; 20 import java.util.HashMap; 21 import java.util.List; 22 import java.util.Map; 23 24 import static junit.framework.TestCase.*; 25 26 @SuppressWarnings({"unchecked"}) 27 public class InvocationImplTest extends TestBase { 28 29 private Invocation invocation; 30 31 @Before setup()32 public void setup() throws Exception { 33 invocation = new InvocationBuilder().args(" ").mock("mock").toInvocation(); 34 } 35 36 @Test shouldKnowIfIsEqualTo()37 public void shouldKnowIfIsEqualTo() { 38 Invocation equal = new InvocationBuilder().args(" ").mock("mock").toInvocation(); 39 Invocation nonEqual = new InvocationBuilder().args("X").mock("mock").toInvocation(); 40 Invocation withNewStringInstance = new InvocationBuilder().args(new String(" ")).mock("mock").toInvocation(); 41 42 assertFalse(invocation.equals(null)); 43 assertFalse(invocation.equals("")); 44 assertTrue(invocation.equals(equal)); 45 assertFalse(invocation.equals(nonEqual)); 46 assertTrue(invocation.equals(withNewStringInstance)); 47 } 48 49 @Test shouldEqualToNotConsiderSequenceNumber()50 public void shouldEqualToNotConsiderSequenceNumber() { 51 Invocation equal = new InvocationBuilder().args(" ").mock("mock").seq(2).toInvocation(); 52 53 assertTrue(invocation.equals(equal)); 54 assertTrue(invocation.getSequenceNumber() != equal.getSequenceNumber()); 55 } 56 57 @Test shouldBeACitizenOfHashes()58 public void shouldBeACitizenOfHashes() { 59 Map<Invocation, String> map = new HashMap<Invocation, String>(); 60 map.put(invocation, "one"); 61 assertEquals("one", map.get(invocation)); 62 } 63 64 @Test shouldPrintMethodName()65 public void shouldPrintMethodName() { 66 invocation = new InvocationBuilder().toInvocation(); 67 assertEquals("iMethods.simpleMethod();", invocation.toString()); 68 } 69 70 @Test shouldPrintMethodArgs()71 public void shouldPrintMethodArgs() { 72 invocation = new InvocationBuilder().args("foo").toInvocation(); 73 Assertions.assertThat(invocation.toString()).endsWith("simpleMethod(\"foo\");"); 74 } 75 76 @Test shouldPrintMethodIntegerArgAndString()77 public void shouldPrintMethodIntegerArgAndString() { 78 invocation = new InvocationBuilder().args("foo", 1).toInvocation(); 79 Assertions.assertThat(invocation.toString()).endsWith("simpleMethod(\"foo\", 1);"); 80 } 81 82 @Test shouldPrintNull()83 public void shouldPrintNull() { 84 invocation = new InvocationBuilder().args((String) null).toInvocation(); 85 Assertions.assertThat(invocation.toString()).endsWith("simpleMethod(null);"); 86 } 87 88 @Test shouldPrintArray()89 public void shouldPrintArray() { 90 invocation = new InvocationBuilder().method("oneArray").args(new int[] { 1, 2, 3 }).toInvocation(); 91 Assertions.assertThat(invocation.toString()).endsWith("oneArray([1, 2, 3]);"); 92 } 93 94 @Test shouldPrintNullIfArrayIsNull()95 public void shouldPrintNullIfArrayIsNull() throws Exception { 96 Method m = IMethods.class.getMethod("oneArray", Object[].class); 97 invocation = new InvocationBuilder().method(m).args((Object) null).toInvocation(); 98 Assertions.assertThat(invocation.toString()).endsWith("oneArray(null);"); 99 } 100 101 @Test shouldPrintArgumentsInMultilinesWhenGetsTooBig()102 public void shouldPrintArgumentsInMultilinesWhenGetsTooBig() { 103 invocation = new InvocationBuilder().args("veeeeery long string that makes it ugly in one line", 1).toInvocation(); 104 Assertions.assertThat(invocation.toString()).endsWith( 105 "simpleMethod(" + 106 "\n" + 107 " \"veeeeery long string that makes it ugly in one line\"," + 108 "\n" + 109 " 1" + 110 "\n" + 111 ");"); 112 } 113 114 @Test shouldTransformArgumentsToMatchers()115 public void shouldTransformArgumentsToMatchers() throws Exception { 116 Invocation i = new InvocationBuilder().args("foo", new String[]{"bar"}).toInvocation(); 117 List matchers = ArgumentsProcessor.argumentsToMatchers(i.getArguments()); 118 119 assertEquals(2, matchers.size()); 120 assertEquals(Equals.class, matchers.get(0).getClass()); 121 assertEquals(ArrayEquals.class, matchers.get(1).getClass()); 122 } 123 124 class Foo { bark()125 public String bark() { 126 return "woof"; 127 } 128 } 129 130 @Test shouldBeAbleToCallRealMethod()131 public void shouldBeAbleToCallRealMethod() throws Throwable { 132 //when 133 Invocation invocation = invocationOf(Foo.class, "bark", new RealMethod() { 134 public Object invoke(Object target, Object[] arguments) throws Throwable { 135 return new Foo().bark(); 136 }}); 137 //then 138 assertEquals("woof", invocation.callRealMethod()); 139 } 140 141 @Test shouldScreamWhenCallingRealMethodOnInterface()142 public void shouldScreamWhenCallingRealMethodOnInterface() throws Throwable { 143 //given 144 Invocation invocationOnInterface = new InvocationBuilder().toInvocation(); 145 146 try { 147 //when 148 invocationOnInterface.callRealMethod(); 149 //then 150 fail(); 151 } catch(MockitoException e) {} 152 } 153 154 @Test shouldReturnCastedArgumentAt()155 public void shouldReturnCastedArgumentAt(){ 156 //given 157 int argument = 42; 158 Invocation invocationOnInterface = new InvocationBuilder().method("twoArgumentMethod"). 159 argTypes(int.class, int.class).args(1, argument).toInvocation(); 160 161 //when 162 int secondArgument = (Integer) invocationOnInterface.getArgument(1); 163 164 //then 165 assertTrue(secondArgument == argument); 166 } 167 } 168