Skip to content

Problem with mocking not overriden method which uses generics #500

Description

@GoogleCodeExporter
What steps will reproduce the problem?
1. Create classes:
abstract class AbstractConverter<F, T> {

    public List<T> convert(List<F> from) {
        List<T> result = new ArrayList<>();
        for (F f : from) {
            result.add(convert(f));
        }
        return result;
    }

    public abstract T convert(F from);
}

public class ConcreteConverter extends AbstractConverter<Integer, String> {

    @Override
    public String convert(Integer from) {
        return from.toString();
    }
}
2. Create Test:
@RunWith(MockitoJUnitRunner.class)
public class MyTest {

    @Mock
    private ConcreteConverter cc;

    @Mock
    private List<Integer> intList;

    @Mock
    private List<String> stringList;

    @Test
    public void test() {
        when(cc.convert(intList)).thenReturn(stringList);
    }
}
3. Run the test.

What is the expected output? What do you see instead?
The test should run flawlessly but instead it fails with NullPointerException. 
It seems that object cc is not mocked correctly because method convert is 
invoked.

What version of the product are you using? On what operating system?
<dependency>
  <groupId>org.mockito</groupId>
  <artifactId>mockito-core</artifactId>
  <version>1.9.5</version>
  <scope>test</scope>
</dependency>
Centos 6.3
java version "1.7.0_65"
OpenJDK Runtime Environment (rhel-2.5.1.2.el6_5-x86_64 u65-b17)
OpenJDK 64-Bit Server VM (build 24.65-b04, mixed mode)

Please provide any additional information below.
The problem can be bypassed by mocking class:
private static class MyConverter extends ConcreteConverter {

    @Override
    public List<String> convert(List<Integer> ints) {
        return super.convert(ints);
    }
}
instead of ConcreteConverter class

Original issue reported on code.google.com by arkadius...@gmail.com on 8 Aug 2014 at 6:54

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions