JMockit - initialization problem

后端 未结 7 1222
情话喂你
情话喂你 2020-12-01 20:33

When I use the following test I get a WARNING:

WARNING: JMockit was initialized on demand, which may cause certain tests to fail; please check the d

相关标签:
7条回答
  • 2020-12-01 21:05

    I had no difficulties running my tests in Maven but I got the same error when I was running them in eclipse.

    The JMockit Eclipse plugin allowed me to run all the test in eclipse without any extra configuration.

    https://marketplace.eclipse.org/content/jmockit-eclipse

    One of the featues of this plugin is

    Automatically adds JMockit jar as -javaagent argument to JUnit launches.

    0 讨论(0)
  • 2020-12-01 21:06

    The accepted answer has fallen a little out of date regarding the links so it's worth mentioning the various solutions directly.

    To fix this problem do one of the following:

    1 - Specifiy a javaagent

    Add this to your JUnit execution environment (for your version):

     -javaagent:path/to/your/jmockit/jmockit-0.998.jar 
    

    2 - configure the Surefire plugin in Maven to avoid it

    Add the following to your Maven configuration (choose your own versions)

    <!-- JMockit must be before JUnit in the classpath -->
    <dependency>
      <groupId>mockit</groupId>
      <artifactId>jmockit</artifactId>
    </dependency>
    <!-- Standard unit testing -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
    </dependency>
    

    Ensure that your Surefire plugin is configured as follows (for your particular versions):

    <plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-surefire-plugin</artifactId>
       <version>2.4.3</version>
       <configuration>
          <argLine>-javaagent:${settings.localRepository}/mockit/jmockit/0.998/jmockit-0.998.jar</argLine>
          <useSystemClassLoader>true</useSystemClassLoader>
        </configuration>
     </plugin>
    

    3 - Use the JUnit @RunWith annotation

    Add this JUnit runner annotation on each and every test class

    @RunWith(JMockit.class)
    public class ExampleTest {}
    
    0 讨论(0)
  • 2020-12-01 21:11

    I setup a properties file in the classpath for easy configuration of Junit 5:

    It MUST be named junit-platform.properties

    junit.jupiter.extensions.autodetection.enabled = true
    junit.jupiter.testinstance.lifecycle.default = per_class
    

    Make sure you're using a newer version of Jmockit that has the JmockitExtension class. NOTE: Jmockit version 1.8 IS NOT newer than version 1.41. The 1.8 version should have been 1.08.

    Maven Central reference: https://mvnrepository.com/artifact/org.jmockit/jmockit

    0 讨论(0)
  • 2020-12-01 21:19

    It still doesnt run for me in IntelliJ. I am able to run it command line though.

    0 讨论(0)
  • 2020-12-01 21:21

    As I understand it, this exception is thrown when one attempts to call a JMockit method, while JMockit has not been properly initialized.

    Make sure you follow the JMockit installation instructions, especially points 3 and 4. If the JMockit jar comes after the JUnit jar in the classpath, it might cause problems.

    0 讨论(0)
  • 2020-12-01 21:22

    I simply added:

    @RunWith(JMockit.class)

    Which resolved the issue, as per the documentation in the accepted answer.

    0 讨论(0)
提交回复
热议问题