Search code examples
javajunit4jmockit

Why won't JMockit use this (apparently) valid Injectable?


I recently refactored one of my classes to accept an iterable of generic objects in its constructor and am now unable to get JMockit to instantiate the @Tested field of the test class. Here's a stripped-down test case which exhibits the same problem:

import java.util.Collections;
import mockit.Injectable;
import mockit.Tested;
import org.junit.Test;

public class FooTest {
    public static interface Generic<T> {}

    public static class Foo<T> implements Generic<T> {
        public Foo(Iterable<Generic<T>> iterable) {}
    }

    @Tested Foo<Object> tested;
    @Injectable Iterable<Generic<Object>> injectable = Collections.emptyList();

    @Test
    public void testFoo() {
        // java.lang.IllegalArgumentException: No constructor in class FooTest$Foo that can be satisfied by available injectables
    }
}

I realize I could trivially work around this by created tested in a @Before method, but I want to understand why this is failing, first. :-)

I'm using Java 1.7.0_51, JMockit 1.8, and JUnit 4.11.


Solution

  • The @Tested feature doesn't fully support generic type parameters yet, as of JMockit 1.8.

    That said, JMockit 1.9 (to be released shortly, Jun 22) adds support for scenarios like this one.