IntelliJ Idea is giving following warning for one of the statements in my test cases.
Warning: Unchecked generics array creation for varargs parameter
Signals places where an unchecked warning is issued by the compiler
All I am doing is :
when(someService.someMethod(id)).thenThrow(AccountNotFoundException.class)
A better way to accomplish that would probably be:
when(someService.someMethod(id)).thenThrow(new AccountNotFoundException());
then the compiler should infer the type correctly.
If someService.someMethod(id) has return type void you can do:
doThrow(new AccountNotFoundException()).when(someService).someMethod(id);
Please refer to the official Mockito documentation for examples like this using thenThrow. The documentation is very clear on this point.
You can also check the following StackOverflow question which is very similar (if not an exact duplicate):
How to mock and assert a thrown exception
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With