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