{"record":{"id":"6fe36bcdc4e6aab9","repo":"OpenFeign/feign","slug":"wanted-s-to-be-invoked-s-times-but-got","errorCode":null,"errorMessage":"Wanted: '%s' to be invoked: '%s' times but got: '%s'!","messagePattern":"Wanted: '(.+?)' to be invoked: '(.+?)' times but got: '(.+?)'!","errorType":"exception","errorClass":"VerificationAssertionError","httpStatus":null,"severity":"error","filePath":"mock/src/main/java/feign/mock/MockClient.java","lineNumber":263,"sourceCode":"  public List<Request> verifyTimes(final HttpMethod method, final String url, final int times) {\n    if (times < 0) {\n      throw new IllegalArgumentException(\"times must be a non negative number\");\n    }\n\n    if (times == 0) {\n      verifyNever(method, url);\n      return Collections.emptyList();\n    }\n\n    RequestKey requestKey = RequestKey.builder(method, url).build();\n    if (!requests.containsKey(requestKey)) {\n      throw new VerificationAssertionError(\n          \"Wanted: '%s' but never invoked! Got: %s\", requestKey, requests.keySet());\n    }\n\n    List<Request> result = requests.get(requestKey);\n    if (result.size() != times) {\n      throw new VerificationAssertionError(\n          \"Wanted: '%s' to be invoked: '%s' times but got: '%s'!\",\n          requestKey, times, result.size());\n    }\n\n    return result;\n  }\n\n  public List<Request> verifyTimes(RequestKey requestKey, final int times) {\n    if (times < 0) {\n      throw new IllegalArgumentException(\"times must be a non negative number\");\n    }\n\n    if (times == 0) {\n      verifyNever(requestKey);\n      return Collections.emptyList();\n    }\n\n    List<Request> result = null;","sourceCodeStart":245,"sourceCodeEnd":281,"githubUrl":"https://github.com/OpenFeign/feign/blob/e2a1e27560a1e68840c34f031afca88b36096e30/mock/src/main/java/feign/mock/MockClient.java#L245-L281","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Adjust the expected times to the actual invocation count shown in the message","Fix the code under test if it genuinely invokes the endpoint the wrong number of times (loops, retries)","Call mockClient.resetRequests() in an @After/@BeforeEach to isolate test state","Use verifyOne(method, url) when exactly one invocation is expected"],"exampleFix":"// before\nmockClient.verifyTimes(HttpMethod.POST, \"/orders\", 1); // was invoked twice\n// after\nmockClient.verifyTimes(HttpMethod.POST, \"/orders\", 2); // or fix duplicate call in code under test","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  mockClient.verifyTimes(HttpMethod.POST, \"/orders\", 1);\n} catch (VerificationAssertionError e) {\n  // message shows wanted vs actual invocation count\n  throw new AssertionError(\"Invocation count mismatch: \" + e.getMessage(), e);\n}","preventionTips":["Call resetRequests() in @BeforeEach/@After to isolate counts","Account for retries/loops in the code under test when choosing times","Use verifyOne for the exactly-once contract"],"tags":["mock","test","verification","invocation-count"],"backgroundTag":"schema-validation-failed","analyzedSha":"e2a1e27560a1e68840c34f031afca88b36096e30","analyzedAt":"2026-09-10T12:37:37.238Z","contentChangedAt":"2026-09-10T12:37:37.238Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}