1 package org.mockitousage.stubbing; 2 3 import org.junit.Test; 4 5 import static org.assertj.core.api.Assertions.assertThat; 6 import static org.mockito.AdditionalAnswers.delegatesTo; 7 import static org.mockito.Mockito.mock; 8 import static org.mockito.Mockito.withSettings; 9 10 public class StubbingWithDelegateVarArgsTest { 11 12 public interface Foo { bar(String baz, Object... args)13 int bar(String baz, Object... args); 14 } 15 16 private static final class FooImpl implements Foo { 17 18 @Override bar(String baz, Object... args)19 public int bar(String baz, Object... args) { 20 return args != null ? args.length : -1; // simple return argument count 21 } 22 23 } 24 25 @Test should_not_fail_when_calling_varargs_method()26 public void should_not_fail_when_calling_varargs_method() { 27 Foo foo = mock(Foo.class, withSettings() 28 .defaultAnswer(delegatesTo(new FooImpl()))); 29 assertThat(foo.bar("baz", 12, "45", 67.8)).isEqualTo(3); 30 } 31 32 @Test should_not_fail_when_calling_varargs_method_without_arguments()33 public void should_not_fail_when_calling_varargs_method_without_arguments() { 34 Foo foo = mock(Foo.class, withSettings() 35 .defaultAnswer(delegatesTo(new FooImpl()))); 36 assertThat(foo.bar("baz")).isEqualTo(0); 37 assertThat(foo.bar("baz", new Object[0])).isEqualTo(0); 38 } 39 40 @Test should_not_fail_when_calling_varargs_method_with_null_argument()41 public void should_not_fail_when_calling_varargs_method_with_null_argument() { 42 Foo foo = mock(Foo.class, withSettings() 43 .defaultAnswer(delegatesTo(new FooImpl()))); 44 assertThat(foo.bar("baz", (Object[]) null)).isEqualTo(-1); 45 } 46 } 47