Skip to content

DistinctVarargsChecker flags incorrectly on Mockito stubbings #6240

Open

Description

@ascopes

I've spotted some issues with this check being overly broad in how it operates. The main one seems to be it gets confused when used with Mocktio.

I have this line in a codebase:

try (var staticFilesMock = mockStatic(Files.class)) {

  // Given
  staticFilesMock.when(() -> Files.copy(any(Path.class), any(Path.class), any()))
      .thenThrow(new IOException("Error"));
}

but this new check is considering the argument matchers to be duplicated. I think it is getting confused on the last any() call which has a type CopyOption and is the first argument to a varargs array of CopyOption....

If I specify the type explicitly, I get the same issue, i.e.

staticFilesMock.when(() -> Files.copy(any(Path.class), any(Path.class), any(CopyOption.class)))
    .thenThrow(new IOException("Error"));

My workaround for now is to disable this check. I'm assuming this is not a valid case being flagged here, having read the docs for the check... if this is intentional would it be possible to document this use case on the check page on GitHub pages?

Many thanks!

Activity

  1. cpovirk commented on Oct 26, 2026

    @cpovirk
    Member

    Thanks. I don't think we'd considered Mockito because most of the methods we flag are static and we have static mocking disabled internally. We have existing logic for detecting Mockito usage in other checks, so it if applied easily here, we might be able to get this fixed. I'll try to find time to have a look.

  2. cpovirk commented on Oct 27, 2026

    @cpovirk
    Member

    #6247 addresses the common case. I'm not sure when we'd get to the more thorough fix (which would handle matchers from classes other than the typical built-in Mockit ones), but if you (or anyone else) would benefit from it, let us know.

  3. ascopes commented on Oct 27, 2026

    @ascopes
    Author

    Thanks, what is the cause of the error exactly in this case? What does it consider to be duplicated?

  4. cpovirk commented on Oct 27, 2026

    @cpovirk
    Member

    Oh, I guess I should look at the error text and see how it could be improved, too... someday... :\

    It's complaining about passing any(Path.class) twice. Files.copy(foo, foo, ...) would likely be a bug, since it would be a no-op. But Files.copy(any(Path.class), any(Path.class), ...) is a different story because it's not a "real" call with "real" arguments.

  5. cpovirk commented on Oct 27, 2026

    @cpovirk
    Member

    (Also, it would also be weird to pass any CopyOption multiple times. That fits better with the "DistinctVarargsChecker" name of the check, but we then generalized the check beyond varargs without updating the name.)

    (@kluever FYI)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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