IntelliJ warning: Unchecked generics array creation for varargs parameter

Nitin S :

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)

David Rawson :

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

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=473552&siteId=1