Error "Missing @Injectable for field" with @Resource annotation #359

Closed
AlexStasko opened this Issue Nov 9, 2016 · 11 comments

Projects

None yet

4 participants

@AlexStasko

I use JMockit 1.28 (tried 1.29)
Java 1.8.92

I work with @Resource annotation. But with name value my tests not working. Without it tests work fine.
Console output:

java.lang.IllegalStateException: Missing @Injectable for field "by.common.model.DummyDomain dummyDomain" in DummyService

Also if using @Autowired with @Qualifier tests work fine.
Test case is here: jmockit-check.zip

I use something like this as workaround:

    @BeforeMethod
    public void setUp() {
        dummyService = new DummyService();
        ReflectionTestUtils.setField(dummyServie, "dummyDomain", mockDummyDomain);
    }

Might be that is a bug, maybe I 'm doing something wrong. Thank you in advance and hope for your help.

Thank you,
Alex

@rliesenfeld rliesenfeld added the question label Nov 9, 2016
@rliesenfeld
Member

The @Injectable field should match the name of the target @Resource ("test"). So, name it "test".

@susau
susau commented Nov 9, 2016

Will try and respond but the question why using @Autowired with @Qualifier works?

@AlexStasko

@rliesenfeld in my project I use names like "subname1.subname2.name" and in this case I cannot name field with dots.

@rliesenfeld
Member

The names need to match, otherwise it's not possible to support a scenario where multiple @Resource's of the same type are injected into a given object.

Name matching can be made more flexible, though. I am thinking of two possible solutions, when given a resource name composed of multiple parts separated by special characters such as "." (dot) or "-" (hyphen):

  1. Use only the last name part. So, if the resource is named "part1.part2.last", then the @Injectable would need to be named "last".
  2. Use the full resource name, but replacing separator characters with the underscore "_" character, which can be used in the name of the injectable: "@Injectable Xyz part1_part2_last".

Which solution would you prefer?

@susau
susau commented Nov 9, 2016

I am tech lead of the project so let me answer.
Var N2 looks definitely better but my previous question is still valid I think.

How possible to support a scenario where multiple @Autowied with @Qualifier the same type are injected into a given object? Because as I mentioned if change in the test case @Resource ("test") to
@Autowired
@Qualifier("test)
(which is same thing from Spring prospective) it works. Might be if multiple @Resource but with different types we can use the same "@Autowired " approach?

@rliesenfeld
Member

I see the same behavior with @Resource(name = "test") and with @Qualifier("test"), as it should be.
For example, the test below passes:

public class ExampleDITest {
    static class CUT1 {
        @Resource(name = "db1")
        DataSource ds;
    }

    static class CUT2 {
        @Autowired @Qualifier("db2")
        DataSource ds;
    }

    @Tested CUT1 cut1;
    @Tested CUT2 cut2;

    @Test
    public void injectNamedResources(@Injectable DataSource db1, @Injectable DataSource db2) {
        assertNotSame(db1, db2);
        assertSame(db1, cut1.ds);
        assertSame(db2, cut2.ds);
    }
}
@rliesenfeld rliesenfeld added enhancement and removed question labels Nov 9, 2016
@rliesenfeld rliesenfeld self-assigned this Nov 9, 2016
@AlexStasko

Thanks for your suggestions. But if we will use solution with underscore we will face with codestyle problems. In this case can we use camelcase solution? Something like this:
name = "part1.part2.name"
field name = "part1Part2Name"

@susau
susau commented Nov 10, 2016 edited

codestyle problems

Good catch, I didn't release that. Camelcase is definitely better

@rliesenfeld
Member

Ok, camel case can be supported too.

@susau
susau commented Nov 11, 2016

Is there any chance for fast release with that fix?
Thanks

@rliesenfeld rliesenfeld added a commit that closed this issue Nov 12, 2016
@rliesenfeld rliesenfeld Enhancement: added support for composite names in injection points us…
…ing @Resource(name), @Named, or Spring's @Qualifier, where annotated names can contain the dot and dash characters, while matching @Injectable/@Tested names use camel case; closes #359.
7d47d2c
@Siarhei-Yarkavy

Sorry, could this new behavior be documented in javadoc and tutorial?

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