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

verify can't differ/handle logging methods with "Object... arguments" in signature #3343

Open
fourhundredfortyfour opened this issue May 15, 2024 · 0 comments

Comments

@fourhundredfortyfour
Copy link

Test code should verify twice the call log.error() with identical parameters, but it is blind for the second call to log.error().
Mockito prints identicals calls but behaves different.
Probably the parameter "Object... arguments" leads the jvm to a different method implementation for log.error() and mockito internally differes between them, but there is not interface in mockito to verify the second call different to the first call.

Test code:

  @Test
  void verifyBug() {
    org.slf4j.Logger log = mock(org.slf4j.Logger.class, withSettings().verboseLogging());

    log.error("Format {}", "param");
    callLogger(log, "Format {}", "param");

    verify(log, times(2)).error("Format {}", "param");
  }

  void callLogger(org.slf4j.Logger log, String format, Object... arguments) {
    log.error(format, arguments);
  }

Mockito states:

############ Logging method invocation #1 on mock/spy ########
logger.error("Format {}", "param");
   invoked: -> at com.eosts.logappender.AlertLoggerTest.verifyBug(AlertLoggerTest.java:65)
   has returned: "null"

############ Logging method invocation #2 on mock/spy ########
logger.error("Format {}", "param");
   invoked: -> at com.eosts.logappender.AlertLoggerTest.callLogger(AlertLoggerTest.java:71)
   has returned: "null"

############ Logging method invocation #3 on mock/spy ########
logger.error("Format {}", "param");
   invoked: -> at com.eosts.logappender.AlertLoggerTest.verifyBug(AlertLoggerTest.java:67)
   has thrown: class org.mockito.exceptions.verification.TooFewActualInvocations with message 
logger.error("Format {}", "param");
Wanted 2 times:
-> at com.eosts.logappender.AlertLoggerTest.verifyBug(AlertLoggerTest.java:67)
But was 1 time:
-> at com.eosts.logappender.AlertLoggerTest.verifyBug(AlertLoggerTest.java:65)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant