user1166528
user1166528

Reputation: 381

java.lang.IllegalStateException: JMockit wasn't properly initialized; check that jmockit.jar precedes junit.jar in the classpath

I am getting below exception when i am trying to runing the Junit testcases. If i am changing the classpath entry order it is working fine but the Jococo coverage is not working.It is getting hanging. Can any please help to fix this issue.

<classpathentry kind="lib" path="libt/junit-4.8.2.jar"/>
<classpathentry kind="lib" path="lib/jmockit.jar"/>
java.lang.IllegalStateException: JMockit wasn't properly initialized; check that jmockit.jar precedes junit.jar in the classpath (if using JUnit; if not, check the documentation)
    at com.amica.bc.testutil.BillingCenterTestUtil$1.<init>(BillingCenterTestUtil.java:18)
at com.amica.bc.testutil.BillingCenterTestUtil.setCurrentUserName(BillingCenterTestUtil.java:18)
    at amica.util.logging.TestAmicaGWLogger.setUp(TestAmicaGWLogger.java:36)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Upvotes: 7

Views: 17142

Answers (4)

Juan Moreno
Juan Moreno

Reputation: 2795

The official guide tells us the following:

JMockit also requires the -javaagent JVM initialization parameter to be used; when using the Maven Surefire plugin for test execution, it's specified as follows:

<plugins>
   <plugin>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.21.0</version> <!-- or some other version -->
      <configuration>
         <argLine>
            -javaagent:${settings.localRepository}/org/jmockit/jmockit/${jmockit.version}/jmockit-${jmockit.version}.jar
         </argLine>
         <!-- ... -->
      </configuration>
   <plugin>
   <!-- ... -->
<plugins> 

More details here and here.

Upvotes: 5

NIKHIL CHAURASIA
NIKHIL CHAURASIA

Reputation: 547

I faced similar problem while running my standalone application which requires both jars junit-4.8.1.jar and jmockit-0.999.4.jar. Below are the steps, I did to make this work :

1. Go to pom.xml. Click on depdendency hierarchy option and type "junit" in search bar. From here you will get to know from which dependency your "junit" is coming. Here I found that "nature-6.51" depdendency was responsible for that in my project (Note it down whatever is urs).

junit dependency

2. Type "jmockit" in search bar and note down the dependency showing here (jmockit-0.999.4)

jmockit dependency

3. Go to pom.xml and search for "nature" (or dependency responsible for junit) and "jmockit" dependency we have noted down in step #1 and #2 respectively. You will see that nature dependency will be listed above jmockit dependency.

4. Now just copy the jmockit dependency and place it just above the nature-6.51 dependency (which I did bcz nature's jar was carrying junit in my project)

This worked for me successfully. :)

Upvotes: 2

Dario Palminio
Dario Palminio

Reputation: 89

SYMPTOM: Error when run maven with Jmockit and junit into java project.

MESSAGE: JMockit wasn't properly initialized; check that jmockit.jar precedes junit.jar in the classpath (if using JUnit; if not, check the documentation)

CAUSES:

Wrong configured in POM.xml file. Jmockit dependencyis is after of junit.

SOLUTIONS:

Edit POM.xml file. The jmockit.jar should be precedes junit.jar then jmockit dependency should be before of junit.

Upvotes: 5

Stim
Stim

Reputation: 1465

The problem in my case was that a test was defined in the main source folder, instead of the test source folder. The dependencies for jmockit and junit we defined in the pom with scope test. Therefore they were not available for this test.

The IDE wasn't that strict during editting and compilation, so no warnings whatsoever, but the tests failed with this same strange error.

Upvotes: 1

Related Questions