Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop varargs collector before invoking a user method. #2736

Merged
merged 1 commit into from Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -235,6 +235,9 @@ public Object invoke(Method method, Object target, Object... arguments)
if (!Modifier.isStatic(method.getModifiers())) {
handle = handle.bindTo(target);
}
if (handle.isVarargsCollector()) {
handle = handle.asFixedArity();
}
try {
return DISPATCHER.invokeWithArguments(handle, arguments);
} catch (Throwable t) {
Expand Down
Expand Up @@ -212,6 +212,15 @@ public void testStaticMockMustUseValidMatchers() {
}
}

@Test
public void testStaticMockVarargs() {
assertEquals("foobar", Dummy.fooVarargs("foo", "bar"));
try (MockedStatic<Dummy> ignored = Mockito.mockStatic(Dummy.class)) {
assertNull(Dummy.fooVarargs("foo", "bar"));
}
assertEquals("foobar", Dummy.fooVarargs("foo", "bar"));
}

static class Dummy {

static String var1 = null;
Expand All @@ -227,5 +236,13 @@ static void fooVoid(String var2) {
static void fooVoid(String var2, String var3) {
var1 = var2;
}

static String fooVarargs(String... args) {
StringBuilder sb = new StringBuilder();
for (String arg : args) {
sb.append(arg);
}
return sb.toString();
}
}
}