Only the first @Injectable is considered when searching for fullyInitialized constructor arguments #341

Closed
Vampire opened this Issue Sep 22, 2016 · 1 comment

Projects

None yet

2 participants

@Vampire
Vampire commented Sep 22, 2016

Having the SUT

public class Foo {
    String foo;
    @Inject
    Bar bar;

    static class Bar {
        public Bar(String bar) {
        }
    }
}

and the test class

public class FooTest {
    @Tested(fullyInitialized = true)
    Foo sut;

    @Injectable
    String foo;

    @Injectable
    String bang;

    @Test
    public void test() {
    }
}

you will get the error java.lang.IllegalArgumentException: No injectable value available for parameter "foo" in constructor Foo$Bar#Bar(String bar), because injectable foo was already consumed for field foo and constructor parameter injection only looks at the first candidate which it sees as already consumed.

If you change the test class to

public class FooTest {
    @Tested(fullyInitialized = true)
    Foo sut;

    @Injectable
    String bang;

    @Injectable
    String foo;

    @Test
    public void test() {
    }
}

it works fine, because for field foo the name-matching injectable foo is used and for the Bar constructor the first found injectable bang is used.

The following of course also works due to the name-matching:

public class FooTest {
    @Tested(fullyInitialized = true)
    Foo sut;

    @Injectable
    String foo;

    @Injectable
    String bar;

    @Test
    public void test() {
    }
}
@rliesenfeld rliesenfeld added the bug label Sep 22, 2016
@rliesenfeld rliesenfeld self-assigned this Sep 22, 2016
@rliesenfeld
Member

Seems like a bug. Thanks for reporting it!

@rliesenfeld rliesenfeld added a commit that closed this issue Sep 25, 2016
@rliesenfeld rliesenfeld Fixed bug where a tested object created recursively through construct…
…or injection was not using injectables consumed only in the parent @Tested object; closes #341.
35384d2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment