Strange behavior with multiple Injectables of the same type #400

Closed
JMmx38965f opened this Issue Mar 10, 2017 · 0 comments

Comments

2 participants
@JMmx38965f

JMockit version: 1.30
TestNG version: 6.9.13

The following code demonstrates the behavior. As it is written, the test will fail with a missing invocation as expected, and "real" is printed to the console. But if the two commented lines are uncommented, the test passes and "mock" is printed to the console. Is this the expected behavior when two Injectables of the same type are within the scope of the test? I've found that it doesn't matter the type of the Injectables either; File, HashMap, and Bar have all produced this behavior.

private class Bar {
    String str;
    
    Bar(String str) {
        this.str = str;
    }
}

private class Foo {
    Optional<ArrayList<Bar>> getList() {
        Bar bar = new Bar("real");
        ArrayList<Bar> list = new ArrayList<Bar>();
        list.add(bar);
        return Optional.of(list);
    }
}
// @Injectable HashMap bar1; // Uncommenting these 2 lines
// @Injectable HashMap bar2; // causes the test to pass
@Injectable Optional<ArrayList<Bar>> optListMock;
@Test
void test() {
    ArrayList<Bar> expList = new ArrayList<Bar>();
    expList.add(new Bar("mock"));
    
    new Expectations(Optional.class) {
        {
            optListMock.get();
            result = expList;
        }
    };
    
    Foo foo = new Foo();
    ArrayList<Bar> list = foo.getList().get();
    System.out.println(list.get(0).str);
}

@rliesenfeld rliesenfeld self-assigned this Mar 11, 2017

@rliesenfeld rliesenfeld added the bug label Mar 11, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment