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

JMockit fails to inject primitive when annotated with @Value #572

michael-pratt opened this issue Jan 2, 2019 · 1 comment

JMockit fails to inject primitive when annotated with @Value #572

michael-pratt opened this issue Jan 2, 2019 · 1 comment


Copy link

@michael-pratt michael-pratt commented Jan 2, 2019

Please provide the following information:

  • Version of JMockit that was used:


  • Description of the problem:

Forgive me if this is working as intended or otherwise not an issue, but after several hours of searching I am unable to find anything to indicate otherwise.

I'm trying to use @Tested on a class that uses Spring's @Value on some properties (specifically, they are integer timeout values). However, JMockit complains that it cannot create the object because it cannot instantiate the Integer field.

It looks like JMockit is trying to use the literal value of Spring's @Value annotation to create an integer, which of course fails since more often than not, that annotation is a property name placeholder.

Sample class to test:

public class DemoService
   private int readTimeout;

   // methods

And test class for it:

public class DemoServiceTest
   DemoService demoService;

   // Test methods

And the first few lines of the stack trace when running the test:

java.lang.RuntimeException: Unable to instantiate class java.lang.Integer with value "${readTimeout}"
    at mockit.internal.util.Utilities.newWrapperInstance(
    at mockit.internal.util.Utilities.convertFromString(
    at mockit.internal.injection.InjectionPoint.getValueFromAnnotation(
    at mockit.internal.injection.field.FieldInjection.getValueForFieldIfAvailable(
    at mockit.internal.injection.field.FieldInjection.injectIntoEligibleFields(
    at mockit.internal.injection.TestedObject.performFieldInjection(

In case it matters, this is with JUnit 5.

I understand I can probably use the Spring Test module to create my beans, but in my real case my service class has tons of autowired dependencies that are much easier to mock using JMockit expectations.

Here's the repo with the code above:

  • Check that:
  • [x ] The problem can be reproduced by JMockit project members.
    For that, include an example test (perhaps accompanied by a Maven/Gradle build script) which
    can be executed without changes and reproduces the failure.

  • [x ] The issue does not fall outside the scope of the project (for example, attempting to use JMockit
    APIs from Groovy or Scala code, or with an Android runtime).

  • [ x] The JDK where the problem occurs is a final release, not a development build.

Copy link

@rliesenfeld rliesenfeld commented Jan 2, 2019

Right, use of @Value on a field should not require a corresponding @Injectable or @Tested value to be provided in tests. Fixing it.

@rliesenfeld rliesenfeld self-assigned this Jan 2, 2019
@rliesenfeld rliesenfeld added the bug label Jan 2, 2019
@jmockit jmockit locked and limited conversation to collaborators Apr 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

No branches or pull requests

2 participants