Search code examples
javajmockitinjectable

JMockit's @Injectable for non-Autowired fields


I have a class in which some fields are @Autowired and some are not, and in the test class I would like to auto-inject values not only for the @Autowired fields. But the auto injection occurs only for @Autowired fields.

Here is an example:

public class SimpleObject {
    Long id;
    @Autowired UsersDAO usersDAO;
}


public class SimpleTest {

    @Tested SimpleObject testedSimpleObject;
    @Injectable @Mocked UsersDAO usersDAO;
    @Injectable Long id = new Long(200);

  @Test
  public void testId() {
      assertNotNull(testedSimpleObject);
      assertNotNull(testedSimpleObject.id);
  }
}

The test will fail on the second line. the id field is not auto-injected. But if I remove the @Autowired annotation from UsersDAO, the test passes.

Why is it so? And how can I overcome the problem? I understand that I can add some other annotation to the id field, but I would like, if possible, to solve it without changing the original source code.


Solution

  • This is due to a bug in JMockit 1.14, but it's already fixed for the next release, 1.15 (due out later on Feb/2015).