简体   繁体   中英

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

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!

I think Java 8 is a bit more type-save since you want to have a MyMapper<T> but you are accessing it with only MyMapper .

Solution could be to use

Mockito.<MyMapper<ConcreteType>>any()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM