Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strange behavior with multiple Injectables of the same type #400

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

Strange behavior with multiple Injectables of the same type #400

JMmx38965f opened this issue Mar 10, 2017 · 0 comments
Assignees
Labels

Comments

@JMmx38965f
Copy link

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants