Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The method in the Type is not applicable for the arguments for varargs method upgrading to Java 1.8

I am using Mocikto framework (version 1.9.5) in my project for unit testing with Java 1.7 now I am migrating my project to build and run with Java 1.8.

In one of my unit test I am trying to mock following method

public <T> List<T> myMethod(final String sql, final MyMapper<T> MyMapper, final Argument... args) 

with this code

String learningId = "testLeaId";
String catalogId = "testCatId";
List<String> returnList = new ArrayList<String>();
returnList.add(catalogId);
when(myService.myMethod(Mockito.anyString(), Mockito.any(MyMapper.class), (Argument[]) Mockito.anyVararg())).thenReturn(returnList);

This code works fine with Java 1.7 now when I upgraded my Java version to 1.8 in my pom.xml I am getting following error...

[ERROR] The method myMethod(String, MyMapper<T>, Argument...) in the type MyService is not applicable for the arguments (String, MyMapper, Argument)
        C:\somepath\MyDaoTest.java:59

Can any one help me to solve this error ? Thanks

like image 478
Ashish Jagtap Avatar asked Mar 02 '17 12:03

Ashish Jagtap


1 Answers

With Java 8, type inference has been improved, thus you should be able to change

Mockito.any(MyMapper.class)

to

Mockity.any()

for example. (assuming you are using a recent version of Mockito itself)

And you can find then ... that anyVarArg() is deprecated, and to use any() for that, too!

like image 161
Tom Bulgur Avatar answered Nov 03 '22 09:11

Tom Bulgur