1 /* 2 * Copyright (c) 2007 Mockito contributors 3 * This program is made available under the terms of the MIT License. 4 */ 5 package org.mockitousage.basicapi; 6 7 import static org.junit.Assert.assertEquals; 8 import static org.junit.Assert.fail; 9 import static org.mockito.Mockito.*; 10 11 import java.util.ArrayList; 12 13 import org.junit.Test; 14 import org.mockito.Mock; 15 import org.mockito.Mockito; 16 import org.mockito.exceptions.verification.NoInteractionsWanted; 17 import org.mockito.exceptions.verification.opentest4j.ArgumentsAreDifferent; 18 import org.mockitoutil.TestBase; 19 20 public class UsingVarargsTest extends TestBase { 21 22 private interface IVarArgs { withStringVarargs(int value, String... s)23 void withStringVarargs(int value, String... s); 24 withStringVarargsReturningString(int value, String... s)25 String withStringVarargsReturningString(int value, String... s); 26 withObjectVarargs(int value, Object... o)27 void withObjectVarargs(int value, Object... o); 28 withBooleanVarargs(int value, boolean... b)29 boolean withBooleanVarargs(int value, boolean... b); 30 foo(Object... objects)31 int foo(Object... objects); 32 } 33 34 @Mock IVarArgs mock; 35 36 @Test shouldStubStringVarargs()37 public void shouldStubStringVarargs() { 38 when(mock.withStringVarargsReturningString(1)).thenReturn("1"); 39 when(mock.withStringVarargsReturningString(2, "1", "2", "3")).thenReturn("2"); 40 41 RuntimeException expected = new RuntimeException(); 42 doThrow(expected).when(mock).withStringVarargs(3, "1", "2", "3", "4"); 43 44 assertEquals("1", mock.withStringVarargsReturningString(1)); 45 assertEquals(null, mock.withStringVarargsReturningString(2)); 46 47 assertEquals("2", mock.withStringVarargsReturningString(2, "1", "2", "3")); 48 assertEquals(null, mock.withStringVarargsReturningString(2, "1", "2")); 49 assertEquals(null, mock.withStringVarargsReturningString(2, "1", "2", "3", "4")); 50 assertEquals(null, mock.withStringVarargsReturningString(2, "1", "2", "9999")); 51 52 mock.withStringVarargs(3, "1", "2", "3", "9999"); 53 mock.withStringVarargs(9999, "1", "2", "3", "4"); 54 55 try { 56 mock.withStringVarargs(3, "1", "2", "3", "4"); 57 fail(); 58 } catch (Exception e) { 59 assertEquals(expected, e); 60 } 61 } 62 63 @Test shouldStubBooleanVarargs()64 public void shouldStubBooleanVarargs() { 65 when(mock.withBooleanVarargs(1)).thenReturn(true); 66 when(mock.withBooleanVarargs(1, true, false)).thenReturn(true); 67 68 assertEquals(true, mock.withBooleanVarargs(1)); 69 assertEquals(false, mock.withBooleanVarargs(9999)); 70 71 assertEquals(true, mock.withBooleanVarargs(1, true, false)); 72 assertEquals(false, mock.withBooleanVarargs(1, true, false, true)); 73 assertEquals(false, mock.withBooleanVarargs(2, true, false)); 74 assertEquals(false, mock.withBooleanVarargs(1, true)); 75 assertEquals(false, mock.withBooleanVarargs(1, false, false)); 76 } 77 78 @Test shouldVerifyStringVarargs()79 public void shouldVerifyStringVarargs() { 80 mock.withStringVarargs(1); 81 mock.withStringVarargs(2, "1", "2", "3"); 82 mock.withStringVarargs(3, "1", "2", "3", "4"); 83 84 verify(mock).withStringVarargs(1); 85 verify(mock).withStringVarargs(2, "1", "2", "3"); 86 try { 87 verify(mock).withStringVarargs(2, "1", "2", "79", "4"); 88 fail(); 89 } catch (ArgumentsAreDifferent e) { 90 } 91 } 92 93 @Test shouldVerifyObjectVarargs()94 public void shouldVerifyObjectVarargs() { 95 mock.withObjectVarargs(1); 96 mock.withObjectVarargs(2, "1", new ArrayList<Object>(), new Integer(1)); 97 mock.withObjectVarargs(3, new Integer(1)); 98 99 verify(mock).withObjectVarargs(1); 100 verify(mock).withObjectVarargs(2, "1", new ArrayList<Object>(), new Integer(1)); 101 try { 102 verifyNoMoreInteractions(mock); 103 fail(); 104 } catch (NoInteractionsWanted e) { 105 } 106 } 107 108 @Test shouldVerifyBooleanVarargs()109 public void shouldVerifyBooleanVarargs() { 110 mock.withBooleanVarargs(1); 111 mock.withBooleanVarargs(2, true, false, true); 112 mock.withBooleanVarargs(3, true, true, true); 113 114 verify(mock).withBooleanVarargs(1); 115 verify(mock).withBooleanVarargs(2, true, false, true); 116 try { 117 verify(mock).withBooleanVarargs(3, true, true, true, true); 118 fail(); 119 } catch (ArgumentsAreDifferent e) { 120 } 121 } 122 123 @Test shouldVerifyWithAny()124 public void shouldVerifyWithAny() { 125 Foo foo = Mockito.mock(Foo.class); 126 foo.varArgs(""); 127 Mockito.verify(foo).varArgs((String[]) Mockito.any()); 128 Mockito.verify(foo).varArgs((String) Mockito.any()); 129 } 130 131 @Test shouldVerifyWithNullVarArgArray()132 public void shouldVerifyWithNullVarArgArray() { 133 Foo foo = Mockito.mock(Foo.class); 134 foo.varArgs((String[]) null); 135 Mockito.verify(foo).varArgs((String[]) Mockito.any()); 136 Mockito.verify(foo).varArgs((String[]) null); 137 } 138 139 public class Foo { varArgs(String... args)140 public void varArgs(String... args) {} 141 } 142 143 interface MixedVarargs { doSomething(String one, String... varargs)144 String doSomething(String one, String... varargs); 145 doSomething(String one, String two, String... varargs)146 String doSomething(String one, String two, String... varargs); 147 } 148 149 @SuppressWarnings("all") 150 @Test 151 // See bug #31 shouldStubCorrectlyWhenMixedVarargsUsed()152 public void shouldStubCorrectlyWhenMixedVarargsUsed() { 153 MixedVarargs mixedVarargs = mock(MixedVarargs.class); 154 when(mixedVarargs.doSomething("hello", (String[]) null)).thenReturn("hello"); 155 when(mixedVarargs.doSomething("goodbye", (String[]) null)).thenReturn("goodbye"); 156 157 String result = mixedVarargs.doSomething("hello", (String[]) null); 158 assertEquals("hello", result); 159 160 verify(mixedVarargs).doSomething("hello", (String[]) null); 161 } 162 163 @SuppressWarnings("all") 164 @Test shouldStubCorrectlyWhenDoubleStringAndMixedVarargsUsed()165 public void shouldStubCorrectlyWhenDoubleStringAndMixedVarargsUsed() { 166 MixedVarargs mixedVarargs = mock(MixedVarargs.class); 167 when(mixedVarargs.doSomething("one", "two", (String[]) null)).thenReturn("hello"); 168 when(mixedVarargs.doSomething("1", "2", (String[]) null)).thenReturn("goodbye"); 169 170 String result = mixedVarargs.doSomething("one", "two", (String[]) null); 171 assertEquals("hello", result); 172 } 173 174 @Test 175 // See bug #157 shouldMatchEasilyEmptyVararg()176 public void shouldMatchEasilyEmptyVararg() throws Exception { 177 // when 178 when(mock.foo(any())).thenReturn(-1); 179 180 // then 181 assertEquals(-1, mock.foo()); 182 } 183 } 184