{"record":{"id":"2d3533b83750ff78","repo":"mockito/mockito","slug":"unfinished-stubbing-detected-here-location-e","errorCode":null,"errorMessage":"Unfinished stubbing detected here:\n${location}\n\nE.g. thenReturn() may be missing.\nExamples of correct stubbing:\n    when(mock.isOk()).thenReturn(true);\n    when(mock.isOk()).thenThrow(exception);\n    doThrow(exception).when(mock).someVoidMethod();\nHints:\n 1. missing thenReturn()\n 2. you are trying to stub a final method, which is not supported\n 3. you are stubbing the behaviour of another mock inside before 'thenReturn' instruction is completed, possibly on another thread\n","messagePattern":"Unfinished stubbing detected here:\n\\$\\{location\\}\n\nE\\.g\\. thenReturn\\(\\) may be missing\\.\nExamples of correct stubbing:\n    when\\(mock\\.isOk\\(\\)\\)\\.thenReturn\\(true\\);\n    when\\(mock\\.isOk\\(\\)\\)\\.thenThrow\\(exception\\);\n    doThrow\\(exception\\)\\.when\\(mock\\)\\.someVoidMethod\\(\\);\nHints:\n 1\\. missing thenReturn\\(\\)\n 2\\. you are trying to stub a final method, which is not supported\n 3\\. you are stubbing the behaviour of another mock inside before 'thenReturn' instruction is completed, possibly on another thread\n","errorType":"exception","errorClass":"UnfinishedStubbingException","httpStatus":null,"severity":"error","filePath":"mockito-core/src/main/java/org/mockito/internal/progress/MockingProgressImpl.java","lineNumber":119,"sourceCode":"        verificationMode = null;\n        return temp;\n    }\n\n    @Override\n    public void stubbingStarted() {\n        validateState();\n        stubbingInProgress = LocationFactory.create();\n    }\n\n    @Override\n    public void validateState() {\n        validateMostStuff();\n\n        // validate stubbing:\n        if (stubbingInProgress != null) {\n            Location temp = stubbingInProgress;\n            stubbingInProgress = null;\n            throw unfinishedStubbing(temp);\n        }\n    }\n\n    private void validateMostStuff() {\n        // State is cool when GlobalConfiguration is already loaded\n        // this cannot really be tested functionally because I cannot dynamically mess up\n        // org.mockito.configuration.MockitoConfiguration class\n        GlobalConfiguration.validate();\n\n        if (verificationMode != null) {\n            Location location = verificationMode.getLocation();\n            verificationMode = null;\n            throw unfinishedVerificationException(location);\n        }\n\n        getArgumentMatcherStorage().validateState();\n    }\n","sourceCodeStart":101,"sourceCodeEnd":137,"githubUrl":"https://github.com/mockito/mockito/blob/5a676bcd9ef9a10db46e1dc34e190eb46faa2687/mockito-core/src/main/java/org/mockito/internal/progress/MockingProgressImpl.java#L101-L137","documentation":"Mockito detects that a stubbing sequence started with when(...) or doXxx(...).when(mock) was never completed. When validateState() runs (at the next mock interaction such as verify or a new stubbing), MockingProgressImpl finds stubbingInProgress still set and reports the location where the incomplete stubbing began. Usually the chain was interrupted by an exception, a misplaced call, or code that invoked another mock inside the stubbing chain.","triggerScenarios":"Calling when(mock.someMethod()) without thenReturn/thenThrow/thenAnswer; stubbing a final method (when() silently fails to intercept); nesting a mock call (e.g. a getter on another mock) inside when() arguments; concurrent stubbing on multiple threads; an exception thrown between when() and thenReturn.","commonSituations":"Stubbing final classes/methods without mockito-inline; IDE autocompleting when(mock) but never adding thenReturn; helpers that call other mocks while a stubbing is in progress; JUnit tests lacking MockitoExtension that would otherwise surface the error earlier; parallel tests sharing mocks.","solutions":["Complete the stubbing chain with thenReturn(...), thenThrow(...), or thenAnswer(...).","If the method is final (or private/static), stop stubbing it or use the mockito-inline/mock maker that supports it.","Remove or hoist any calls to other mocks that happen inside the when(...) argument list; compute those values before when().","Ensure each thread stubs its own mocks; don't share mock state across threads.","Check that no exception/early return occurs between when(...) and thenReturn(...) in refactored code."],"exampleFix":"// before\nwhen(userRepo.findById(1L)); // unfinished\n// after\nwhen(userRepo.findById(1L)).thenReturn(Optional.of(user));","handlingStrategy":"validation","validationCode":"if (!mock.getClass().getMethod(\"isOk\").getModifiers() isFinalIgnored) { /* ensure every when(...) line is followed by a then*/ } // simple habit: write when(mock.x()).thenReturn(v) on one line","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always write the full stubbing chain on a single line or clearly chained expression.","Never call other mocks inside when(...) argument lists; precompute those values.","Use mockito-inline (or the inline mock maker) if you must mock final methods, otherwise avoid them.","Run MockitoExtension on every test class so incomplete stubbing fails fast with a clear location."],"tags":["mockito","stubbing","unfinished-stubbing","testing"],"backgroundTag":"mockito-unfinished-stubbing","analyzedSha":"5a676bcd9ef9a10db46e1dc34e190eb46faa2687","analyzedAt":"2026-09-05T19:32:58.607Z","contentChangedAt":"2026-09-05T19:32:58.607Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}