Home | History | Annotate | Download | only in varargs
      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.bugs.varargs;
      7 
      8 import org.junit.Test;
      9 import org.mockito.Mock;
     10 import org.mockitoutil.TestBase;
     11 
     12 import static org.junit.Assert.assertEquals;
     13 import static org.junit.Assert.fail;
     14 import static org.mockito.Mockito.*;
     15 
     16 //see issue 62
     17 public class VarargsNotPlayingWithAnyObjectTest extends TestBase {
     18 
     19     interface VarargMethod {
     20         Object run(String... args);
     21     }
     22 
     23     @Mock VarargMethod mock;
     24 
     25     @Test
     26     public void shouldMatchAnyVararg() {
     27         mock.run("a", "b");
     28 
     29         verify(mock).run(anyString(), anyString());
     30         verify(mock).run((String) anyObject(), (String) anyObject());
     31 
     32         verify(mock).run((String[]) anyVararg());
     33 
     34         verify(mock, never()).run();
     35         verify(mock, never()).run(anyString(), eq("f"));
     36     }
     37 
     38     //we cannot use anyObject() for entire varargs because it makes the verification pick up extra invocations
     39     //see other tests in this package
     40     @Test
     41     public void shouldNotAllowUsingAnyObjectForVarArgs() {
     42         mock.run("a", "b");
     43 
     44         try {
     45             verify(mock).run((String[]) anyObject());
     46             fail();
     47         } catch (AssertionError e) {}
     48     }
     49 
     50     @Test
     51     public void shouldStubUsingAnyVarargs() {
     52         when(mock.run((String[]) anyVararg())).thenReturn("foo");
     53 
     54         assertEquals("foo", mock.run("a", "b"));
     55     }
     56 }
     57