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

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

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

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

Comments

@michael-pratt
Copy link

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

Please provide the following information:

  • Version of JMockit that was used:

1.39

  • 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:

@Service
public class DemoService
{
   @Value("${readTimeout}")
   private int readTimeout;

   // methods
}

And test class for it:

public class DemoServiceTest
{
   @Tested
   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(Utilities.java:176)
    at mockit.internal.util.Utilities.convertFromString(Utilities.java:136)
    at mockit.internal.injection.InjectionPoint.getValueFromAnnotation(InjectionPoint.java:249)
    at mockit.internal.injection.field.FieldInjection.getValueForFieldIfAvailable(FieldInjection.java:143)
    at mockit.internal.injection.field.FieldInjection.injectIntoEligibleFields(FieldInjection.java:78)
    at mockit.internal.injection.TestedObject.performFieldInjection(TestedObject.java:147)

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: https://github.com/michael-pratt/jmockit-inject-value

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

@rliesenfeld

This comment has been minimized.

Copy link
Member

@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.
2 participants
You can’t perform that action at this time.