Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntelliJ warning: Unchecked generics array creation for varargs parameter

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)

like image 897
Nitin S Avatar asked Jun 09 '18 22:06

Nitin S


1 Answers

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

like image 200
David Rawson Avatar answered Sep 20 '22 11:09

David Rawson