Skip to content

Remove deprecated VarargMatcher interface in v5 #2836

@big-andy-coates

Description

@big-andy-coates

VarargMatcher is an internal interface used to match some matchers as 'varargs aware'.

When MatcherApplicationStrategy sees a VarargMatcher passed as the last matcher to a varargs method, it will use this matcher to match each value passed to the varargs parameter.

This approach has been identified as broken, as it does not allow users to differentiate between matching an invocation where a single value is passed to the varargs parameter from an invocation matching any number of parameters.

// For example, given:
String varargsMethod(String... args);

when(mock.varargsMethod(any()).thenReturn("any()");
when(mock.varargsMethod(any(), any()).thenReturn("any(), any()");

// Then:
// The following invocation will match the second mocking and therefore return "any(), any()"
mock.varargsMethod("1", "2");

// So far so good, ...but all the following invocations will match the first mocking and therefore return "any()":
mock.varargsMethod();
mock.varargsMethod("1");
mock.varargsMethod("1", "2", "3");

// i.e. there is no way to say "match exactly 1 param"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions