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