Home  >  Article  >  Java  >  A little tip about Java generics

A little tip about Java generics

高洛峰
高洛峰Original
2016-11-16 10:12:011804browse

A small tip about Java generics

Use ArgumentCaptor to capture the parameters in the mock method as shown below

ArgumentCaptor<List<Foo>> fooCaptor = ArgumentCaptor.forClass(List.class);

Error reporting

Error:(89, 86) java: incompatible types: org.mockito.ArgumentCaptor<java.util.List> cannot be converted to org.mockito.ArgumentCaptor<java.util.List<com.myapp.model.Foo>>

Solution

Using a higher version of Mockito--mockito-core-2.0.36-beta

Code comparison

mockito-core-1.0.19

public static <T> ArgumentCaptor<T> forClass(Class<T> clazz) {
    return new ArgumentCaptor<T>(clazz);
}

mockito-core-2.0.36-beta

public static <U,S extends U> ArgumentCaptor<U> forClass(Class<S> clazz) {
    return new ArgumentCaptor<U>(clazz);
}


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn