{"action":"closed","issue":{"url":"https://api.github.com/repos/mockito/mockito/issues/1269","repository_url":"https://api.github.com/repos/mockito/mockito","labels_url":"https://api.github.com/repos/mockito/mockito/issues/1269/labels{/name}","comments_url":"https://api.github.com/repos/mockito/mockito/issues/1269/comments","events_url":"https://api.github.com/repos/mockito/mockito/issues/1269/events","html_url":"https://github.com/mockito/mockito/issues/1269","id":277076676,"number":1269,"title":"Consecutive stubbing fails when using `thenThrow` at first","user":{"login":"eder7","id":1751937,"avatar_url":"https://avatars2.githubusercontent.com/u/1751937?v=4","gravatar_id":"","url":"https://api.github.com/users/eder7","html_url":"https://github.com/eder7","followers_url":"https://api.github.com/users/eder7/followers","following_url":"https://api.github.com/users/eder7/following{/other_user}","gists_url":"https://api.github.com/users/eder7/gists{/gist_id}","starred_url":"https://api.github.com/users/eder7/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/eder7/subscriptions","organizations_url":"https://api.github.com/users/eder7/orgs","repos_url":"https://api.github.com/users/eder7/repos","events_url":"https://api.github.com/users/eder7/events{/privacy}","received_events_url":"https://api.github.com/users/eder7/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2017-11-27T16:15:59Z","updated_at":"2017-11-27T16:36:11Z","closed_at":"2017-11-27T16:36:11Z","author_association":"NONE","body":"Hi,\r\n\r\nbasically it's possible to change one stub's behaviour by again stubbing the same method of the same mock. Here an example:\r\n\r\n```\r\nwhen(mock.method()).thenReturn(\"a\");\r\n// some asserts here\r\nwhen(mock.method()).thenReturn(\"b\");\r\n```\r\n\r\nBut when you do this...\r\n\r\n```\r\nwhen(mock.method()).thenThrow(new IllegalStateException(\"no no no!\"));\r\n// some asserts here\r\nwhen(mock.method()).thenReturn(\"b\");\r\n```\r\n\r\n...it throws the `IllegalStateException` right away during stubbing. Note that it's just a problem with `thenThrow`, not when you use `thenReturn` as the first line. This is due to mockito calling the stub answer instead of just capturing a call (to `method()`). That makes no sense in my opinion. During stubbing there should be no execution of an answer defined in a previous stubbing.\r\n\r\nI know that it's considered bad test design. A mock's behaviour should be defined once and then it's just used. But in more complex workflows it makes absolute sense. I also know there is the possibility to define the behaviour of consecutive calls to the same method (`doThrow(...).doReturn(...)`) but it's not the same. When you have a framework around mocking tasks where you want to abstract away a layer of test setup you might end up with the need to changing the behaviour of a stub (which is actually the case here).\r\n\r\nAfter all, there is no point i see (please correct me if I'm wrong) to call the stub answer during stubbing.\r\n\r\nTo reproduce you can use this code (`mockito-core:2.12.0` was used):\r\n\r\n```\r\nclass MockitoTest {\r\n    \r\n    class Subject {\r\n        fun validate() = \"OK\"\r\n    }\r\n\r\n    @Test\r\n    fun test1() {\r\n        val m = mock(Subject::class.java)\r\n        `when`(m.validate()).thenThrow(IllegalStateException(\"x\"))\r\n    }\r\n\r\n    @Test\r\n    fun test2() {\r\n        val m = mock(Subject::class.java)\r\n        `when`(m.validate()).thenThrow(IllegalStateException(\"x\"))\r\n        `when`(m.validate()).thenReturn(\"OK\")\r\n    }\r\n\r\n}\r\n```\r\n\r\nBy the way: The same problem we can see at this question at Stackoverflow: https://stackoverflow.com/questions/4180209/mockito-re-stub-method-already-stubbed-with-thenthrow\r\n\r\nThe solution there was to use `reset()` but this resets *all* stubs. Not really what you'd want usually in that case..."}}
{"actor":{"display_login":"TimvdLippe"}}