{"action":"closed","issue":{"url":"https://api.github.com/repos/mockito/mockito/issues/1522","repository_url":"https://api.github.com/repos/mockito/mockito","labels_url":"https://api.github.com/repos/mockito/mockito/issues/1522/labels{/name}","comments_url":"https://api.github.com/repos/mockito/mockito/issues/1522/comments","events_url":"https://api.github.com/repos/mockito/mockito/issues/1522/events","html_url":"https://github.com/mockito/mockito/issues/1522","id":372564895,"node_id":"MDU6SXNzdWUzNzI1NjQ4OTU=","number":1522,"title":"Correct usage of strict stubbings -> PotentialStubbingProblem: Strict stubbing argument mismatch","user":{"login":"Gwynnie","id":782632,"node_id":"MDQ6VXNlcjc4MjYzMg==","avatar_url":"https://avatars3.githubusercontent.com/u/782632?v=4","gravatar_id":"","url":"https://api.github.com/users/Gwynnie","html_url":"https://github.com/Gwynnie","followers_url":"https://api.github.com/users/Gwynnie/followers","following_url":"https://api.github.com/users/Gwynnie/following{/other_user}","gists_url":"https://api.github.com/users/Gwynnie/gists{/gist_id}","starred_url":"https://api.github.com/users/Gwynnie/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Gwynnie/subscriptions","organizations_url":"https://api.github.com/users/Gwynnie/orgs","repos_url":"https://api.github.com/users/Gwynnie/repos","events_url":"https://api.github.com/users/Gwynnie/events{/privacy}","received_events_url":"https://api.github.com/users/Gwynnie/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-10-22T15:17:49Z","updated_at":"2018-11-25T16:44:14Z","closed_at":"2018-11-25T16:44:14Z","author_association":"NONE","body":"Hi guys,\r\n\r\nThis is following is almost a copy paste from the mockito google group/forum, which I was recommended to do:\r\nhttps://groups.google.com/forum/#!topic/mockito/WDKfavtJkCg\r\n\r\n**Relevant Versions:**\r\n```\r\nOS : Fedora Linux 4.18.14-200.fc28.x86_64\r\nmockito : 2.23.0\r\njunit : 5.3.1\r\nJDK : Oracle 10.0.2 (happens under 8 and OpenJDK 11.0.1 too)\r\nIntelliJ : 2018.3 EAP\r\n\r\n    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.3.1'\r\n    testRuntime group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.3.1'\r\n    testCompile group: 'org.mockito', name: 'mockito-junit-jupiter', version: '2.23.0'\r\n```\r\n**Actual Question:**\r\n\r\nI had some questions regarding use cases of new \"Strict\" stubbing.\r\n\r\nWith Junit5 & `@ExtendWith(MockitoExtension.class)` I've noticed that it errors out when I think it shouldn't (when compared with Junit4 & `@RunWith(MockitoJUnitRunner.class)` )\r\nI have a very simple test case, which I consider to be both good code, and good mocking. Which gets highlighted as incorrect stubbing usage under Strict (default) checking\r\n\r\n```\r\npackage com.example;\r\n\r\nimport org.junit.jupiter.api.Test;\r\nimport org.junit.jupiter.api.extension.ExtendWith;\r\nimport org.mockito.Mock;\r\nimport org.mockito.junit.jupiter.MockitoExtension;\r\n\r\nimport java.util.Map;\r\n\r\nimport static org.junit.jupiter.api.Assertions.assertEquals;\r\nimport static org.mockito.Mockito.when;\r\n\r\n@ExtendWith(MockitoExtension.class)\r\nclass MockitoTest {\r\n\r\n    @Mock\r\n    private Map<String, Object> mockMap;\r\n    @Mock\r\n    private Object mockObject;\r\n\r\n    @Test\r\n    void shouldBeAbleToMock() {\r\n        // When\r\n        when(mockMap.get(\"A\")).thenReturn(\"Apple\");\r\n        when(mockMap.get(\"B\")).thenReturn(mockObject);\r\n\r\n        // Then\r\n        assertEquals(\"Apple\", mockMap.get(\"A\"));\r\n        assertEquals(mockObject, mockMap.get(\"B\"));\r\n    }\r\n}\r\n```\r\n\r\nThe above gives the following stacktrace when run:\r\n```\r\norg.mockito.exceptions.misusing.PotentialStubbingProblem: \r\nStrict stubbing argument mismatch. Please check:\r\n - this invocation of 'get' method:\r\n    mockMap.get(\"B\");\r\n    -> at com.example.MockitoTest.shouldBeAbleToMock(MockitoTest.java:25)\r\n - has following stubbing(s) with different arguments:\r\n    1. mockMap.get(\"A\");\r\n      -> at com.example.MockitoTest.shouldBeAbleToMock(MockitoTest.java:24)\r\nTypically, stubbing argument mismatch indicates user mistake when writing tests.\r\nMockito fails early so that you can debug potential problem easily.\r\nHowever, there are legit scenarios when this exception generates false negative signal:\r\n  - stubbing the same method multiple times using 'given().will()' or 'when().then()' API\r\n    Please use 'will().given()' or 'doReturn().when()' API for stubbing.\r\n  - stubbed method is intentionally invoked with different arguments by code under test\r\n    Please use default or 'silent' JUnit Rule (equivalent of Strictness.LENIENT).\r\nFor more information see javadoc for PotentialStubbingProblem class.\r\n\r\n    at com.example.MockitoTest.shouldBeAbleToMock(MockitoTest.java:25)\r\n    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\r\n    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)\r\n    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\r\n    at java.base/java.lang.reflect.Method.invoke(Method.java:564)\r\n    at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:515)\r\n    at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:115)\r\n    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:171)\r\n    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)\r\n    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:167)\r\n    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:114)\r\n    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:59)\r\n    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:105)\r\n    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)\r\n    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:95)\r\n    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:71)\r\n    at java.base/java.util.ArrayList.forEach(ArrayList.java:1378)\r\n    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)\r\n    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:110)\r\n    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)\r\n    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:95)\r\n    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:71)\r\n    at java.base/java.util.ArrayList.forEach(ArrayList.java:1378)\r\n    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)\r\n    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:110)\r\n    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)\r\n    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:95)\r\n    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:71)\r\n    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)\r\n    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)\r\n    at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)\r\n    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:220)\r\n    at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:188)\r\n    at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:202)\r\n    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:181)\r\n    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128)\r\n    at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:74)\r\n    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)\r\n    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)\r\n    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)\r\n```\r\n\r\nI've found 1 other reference to this issue online, which was here:\r\nhttps://stackoverflow.com/questions/52139619/simulation-of-service-using-mockito-2-leads-to-stubbing-error\r\n\r\nThe above stated you could work around the issue by changing the code as follows:\r\n```\r\n// From this\r\nwhen(mockMap.get(\"A\")).thenReturn(\"Apple\");\r\nwhen(mockMap.get(\"B\")).thenReturn(mockObject);\r\n\r\n// To this\r\nwhen(mockMap.get(anyString())).thenAnswer(invocation -> {\r\n    Object arg = invocation.getArgument(0);\r\n    if (\"A\".equals(arg)) return \"Apple\";\r\n    if (\"B\".equals(arg)) return system;\r\n});\r\n```\r\n\r\nIs this a bug, or is this intentional going forwards?\r\n\r\nThanks for your time!\r\n\r\n\r\ncheck that\r\n\r\n - [✓] The mockito message in the stacktrace have useful information, but it didn't help\r\n - [✓] The problematic code (if that's possible) is copied here;\r\n       Note that some configuration are impossible to mock via Mockito\r\n - [✓] Provide versions (mockito / jdk / os / any other relevant information)\r\n - [✓] Provide a [Short, Self Contained, Correct (Compilable), Example](http://sscce.org) of the issue\r\n       (same as any question on stackoverflow.com)\r\n - [✓] Read the [contributing guide](https://github.com/mockito/mockito/blob/master/.github/CONTRIBUTING.md)\r\n\r\n\r\n"}}
{"actor":{"display_login":"mockitoguy"}}