Skip to content

Analyzer plugin rule: proper use of named argument matchers #183

Open
@srawlins

Description

@srawlins

Unfortunately, mockito cannot catch a bug like this during runtime:

when(a.m1(x: argThat(contains('foo'), 'y'), y: argThat(contains('bar'), 'x')))
    .thenReturn(0);

noSuchMethod receives an arg (null) for x, and an argument matcher for x (contains('foo')), and an arg (null) for y, and an argument matcher for y (contains('bar')), so it thinks everything is kosher.

In addition, mockito does detect that the following are bugs, and reports them, but it would be much more helpful if the user's IDE reported the bug earlier:

// `any` should be `anyNamed('x')`.
when(a.m1(x: any)).thenReturn(0);

// `anyNamed('y')` should be `anyNamed('x')`.
when(a.m1(x: anyNamed('y'))).thenReturn(0);

// `anyNamed('x')` should be `any`.
when(a.m1(anyNamed('x'))).thenReturn(0);

// `argThat(equals(7))` should be `argThat(equals(7), named: 'x')`.
when(a.m1(x: argThat(equals(7))).thenReturn(0);

// `captureAny` should be `captureAnyNamed('x')`.
verify(a.m1(x: captureAny));

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2A bug or feature request we're likely to work onS2type-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions