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

AlexStasko opened this Issue Nov 9, 2016 · 11 comments


None yet

4 participants


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:

I use something like this as workaround:

    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,

@rliesenfeld rliesenfeld added the question label Nov 9, 2016

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

susau commented Nov 9, 2016

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


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


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 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
(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?


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;

    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

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 = ""
field name = "part1Part2Name"

susau commented Nov 10, 2016 edited

codestyle problems

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


Ok, camel case can be supported too.

susau commented Nov 11, 2016

Is there any chance for fast release with that fix?

@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.

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