Throw exception instead of returning default value

Viewed 3597

I'm new to Mockito and want to use it in unit tests.

What I don't like is mocks created with Mockito.mock(Class<T>) return default values (like null) for methods that have no behavior explicitly defined. Instead, I want them to throw an exception in this case so I know that I need to add this definition.

I tried the following:

SomeType m = mock( SomeType.class, new ThrowsException( new SomeRuntimeException( ... ) ) );
when( m.a() ).thenReturn( ... );

m.a();  // ok
m.b();  // throws exception

But that doesn't work because the exception is thrown already during the call to when().

Is there some other way to achieve this?

1 Answers
Related