Home > Java > javaTutorial > body text

A little tip about Java generics

高洛峰
Release: 2016-11-16 10:12:01
Original
1822 people have browsed it

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);
Copy after login

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>>
Copy after login

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);
}
Copy after login

mockito-core-2.0.36-beta

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


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template