{"record":{"id":"eaba0f6972edb123","repo":"mockito/mockito","slug":"only-void-methods-can-donothing-example-of-corr","errorCode":null,"errorMessage":"Only void methods can doNothing()!\nExample of correct use of doNothing():\n    doNothing().\n    doThrow(new RuntimeException())\n    .when(mock).someVoidMethod();\nAbove means:\nsomeVoidMethod() does nothing the 1st time but throws an exception the 2nd time is called","messagePattern":"Only void methods can doNothing\\(\\)!\nExample of correct use of doNothing\\(\\):\n    doNothing\\(\\)\\.\n    doThrow\\(new RuntimeException\\(\\)\\)\n    \\.when\\(mock\\)\\.someVoidMethod\\(\\);\nAbove means:\nsomeVoidMethod\\(\\) does nothing the 1st time but throws an exception the 2nd time is called","errorType":"exception","errorClass":"MockitoException","httpStatus":null,"severity":"error","filePath":"mockito-core/src/main/java/org/mockito/internal/stubbing/answers/DoesNothing.java","lineNumber":35,"sourceCode":"    private static final long serialVersionUID = 4840880517740698416L;\n\n    private static final DoesNothing SINGLETON = new DoesNothing();\n\n    private DoesNothing() {}\n\n    public static DoesNothing doesNothing() {\n        return SINGLETON;\n    }\n\n    @Override\n    public Object answer(InvocationOnMock invocation) {\n        return null;\n    }\n\n    @Override\n    public void validateFor(InvocationOnMock invocation) {\n        if (!new InvocationInfo(invocation).isVoid()) {\n            throw onlyVoidMethodsCanBeSetToDoNothing();\n        }\n    }\n}\n","sourceCodeStart":17,"sourceCodeEnd":39,"githubUrl":"https://github.com/mockito/mockito/blob/5a676bcd9ef9a10db46e1dc34e190eb46faa2687/mockito-core/src/main/java/org/mockito/internal/stubbing/answers/DoesNothing.java#L17-L39","documentation":"Error \"Only void methods can doNothing()!\nExample of correct use of doNothing():\n    doNothing().\n    doThrow(new RuntimeException())\n    .when(mock).someVoidMethod();\nAbove means:\nsomeVoidMethod() does nothing the 1st time but throws an exception the 2nd time is called\" thrown in mockito/mockito.","triggerScenarios":"Thrown at mockito-core/src/main/java/org/mockito/internal/stubbing/answers/DoesNothing.java:35 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Apply doNothing() only to void methods: doNothing().when(mock).someVoidMethod()","For non-void methods, use doReturn(value).when(mock).method() instead","Check for overloaded methods to ensure you are stubbing the intended signature"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"5a676bcd9ef9a10db46e1dc34e190eb46faa2687","analyzedAt":"2026-09-05T19:32:58.607Z","contentChangedAt":"2026-09-05T19:32:58.607Z","schemaVersion":2},"datasetVersion":"2026-09-12T22:17:10.623Z"}