Mockito test a void method throws an exception
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
When a mocked void method should throw an exception, Mockito uses the doThrow(...).when(mock).method(...) style rather than the when(...).thenThrow(...) form used for non-void methods. The other important part of the test is asserting that your production code reacts correctly to that exception instead of just proving that the mock can throw.
Why Void Methods Need Different Syntax
Mockito's usual stubbing style looks like this:
That does not work for void methods because there is no return value to wrap in when(...). For void methods, use doThrow().
Basic Example
Suppose you have a collaborator with a void method:
To make that method throw during a test:
That is the core Mockito pattern for a void method that throws.
Testing Your Own Code, Not Just the Mock
Usually the more valuable test exercises a class that depends on the mocked service.
Test:
Now the test verifies application behavior, not just Mockito syntax.
You Can Verify the Call Too
Sometimes you want both behaviors: the method should be called, and the call should fail.
That can be useful when the exception path is part of a larger interaction sequence.
doAnswer Is Useful for More Complex Void Behavior
If the void method should throw only under certain argument conditions or needs custom side effects, doAnswer is another option:
That is helpful when you want the mock behavior to depend on the actual invocation.
Prefer assertThrows with Modern JUnit
Older examples often use @Test(expected = ...), but in modern JUnit 5, assertThrows is clearer because the assertion lives next to the code under test.
It also makes it easier to inspect the exception message if needed.
Common Pitfalls
- Using
when(...).thenThrow(...)on a void method is the classic Mockito mistake; usedoThrow(...).when(...)instead. - Testing only that the mock throws can be low value if the real goal is to test how your service reacts to the failure.
- Relying on outdated JUnit syntax makes examples harder to maintain in modern codebases.
- Stubbing one argument value and calling the method with a different one means the exception will not be thrown as expected.
- Forgetting that void methods can still be verified with
verify(...)often leads to weaker tests than necessary.
Summary
- For a void method that should throw, use
doThrow(...).when(mock).voidMethod(...). - Use
assertThrowsto verify the exception path cleanly. - Prefer tests that exercise your own class's behavior when the mocked collaborator fails.
- '
doAnsweris useful when the thrown exception depends on the actual call arguments.' - '
verify(...)still works with void methods and is often useful alongside exception assertions.'
Related reading
- Mockito Trying to spy on method is calling the original method
- Mockito. Verify method arguments
- Mockito verify order / sequence of method calls
- Mockito.any() pass Interface with Generics
- ''Module was not compiled for testing'' when using testable
- ''Module was not compiled for testing'' when using testable
- Models passed to fit can only have training and the first argument in call as positional arguments, found
- model.summary can't print output shape while using subclass model

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.