OpenFeign/feign · error · VerificationAssertionError

Wanted: ' ' to be invoked: ' ' times but got: ' '!

Error message

Wanted: '%s' to be invoked: '%s' times but got: '%s'!

What it means

After finding the recorded requests for a method/url key, verifyTimes compares the number of recorded invocations with the expected times. A mismatch throws a VerificationAssertionError stating how many times the request was wanted versus actually invoked. This is the 'wrong invocation count' verification failure.

Solutions

  1. Adjust the expected times to the actual invocation count shown in the message
  2. Fix the code under test if it genuinely invokes the endpoint the wrong number of times (loops, retries)
  3. Call mockClient.resetRequests() in an @After/@BeforeEach to isolate test state
  4. Use verifyOne(method, url) when exactly one invocation is expected

Example fix

// before
mockClient.verifyTimes(HttpMethod.POST, "/orders", 1); // was invoked twice
// after
mockClient.verifyTimes(HttpMethod.POST, "/orders", 2); // or fix duplicate call in code under test
Defensive patterns

Strategy: try-catch

Try / catch

try {
  mockClient.verifyTimes(HttpMethod.POST, "/orders", 1);
} catch (VerificationAssertionError e) {
  // message shows wanted vs actual invocation count
  throw new AssertionError("Invocation count mismatch: " + e.getMessage(), e);
}

Prevention

When it happens

Trigger: mockClient.verifyTimes(method, url, n) where the requests map contains the key but result.size() != n (e.g. expected 1, invoked 2 or 0-matching-but-different).

Common situations: Retry logic fired extra requests; loop in the code under test invoked the endpoint more times than expected; a previous test state leaked because verifyStatus/resetRequests was not called; expected count off by one.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


AI-assisted analysis of OpenFeign/feign@e2a1e27560 (2026-09-10). Data as JSON: /api/errors/6fe36bcdc4e6aab9. Report an issue: GitHub.

Appendix: source

Thrown at mock/src/main/java/feign/mock/MockClient.java:263

  public List<Request> verifyTimes(final HttpMethod method, final String url, final int times) {
    if (times < 0) {
      throw new IllegalArgumentException("times must be a non negative number");
    }

    if (times == 0) {
      verifyNever(method, url);
      return Collections.emptyList();
    }

    RequestKey requestKey = RequestKey.builder(method, url).build();
    if (!requests.containsKey(requestKey)) {
      throw new VerificationAssertionError(
          "Wanted: '%s' but never invoked! Got: %s", requestKey, requests.keySet());
    }

    List<Request> result = requests.get(requestKey);
    if (result.size() != times) {
      throw new VerificationAssertionError(
          "Wanted: '%s' to be invoked: '%s' times but got: '%s'!",
          requestKey, times, result.size());
    }

    return result;
  }

  public List<Request> verifyTimes(RequestKey requestKey, final int times) {
    if (times < 0) {
      throw new IllegalArgumentException("times must be a non negative number");
    }

    if (times == 0) {
      verifyNever(requestKey);
      return Collections.emptyList();
    }

    List<Request> result = null;

View on GitHub (pinned to e2a1e27560)