5

I am using junit 4.12, jmockit 1.19 & spring-test 4.1.1.RELEASE for testing my Spring MVC Java project.

Order of these dependencies in my pom.xml:

  • jmockit
  • junit
  • spring-test

Test cases for service layer is working fine. I am using spring-test only for testing controllers. While testing controller, I got the following error:

java.lang.IllegalStateException: JMockit wasn't properly initialized; please ensure that jmockit precedes junit in the runtime classpath, or use @RunWith(JMockit.class)

For service layer, I resolved this error by using @RunWith(JMockit.class ) on test class.

But for controller I need to annotate with @RunWith(SpringJUnit4ClassRunner.class ).

How to resolve this error?

Note: I put jmockit before junit in pom.xml

Dev
  • 13,492
  • 19
  • 81
  • 174
  • If you can upgrade to Spring Framework 4.2.1, there is now first-class support for JUnit _Rules_. So you don't need the `SpringJUnit4ClassRunner`: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/integration-testing.html#testcontext-junit4-rules – Sam Brannen Sep 28 '15 at 20:58
  • 1
    I believe you would need to show an example test class and an example pom.xml file that reproduces the issue. I have tested this combination of tools (jmockit, junit, and spring-test), but saw no initilalization failures. – Rogério Sep 29 '15 at 15:11

3 Answers3

0

I'm not entirely sure this will resolve your problem as I just started using jmockit myself and running into similar style issues.

Have you tried using -javaagent:? Easiest way to apply to all JUnit tests (Eclipse) is to go to your JRE settings and add as a VM Argument (Preferences->JRE->Edit->VM Defaults).

Additionally, I found an Eclipse JMockit plugin via the Marketplace that will automatically append the javaagent to any junit tests to avoid the JRE configuration.

Finally, in your POM, you can try (although I haven't tested it myself yet):

        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <argLine>-javaagent:${org.jmockit:jmockit:jar}</argLine>
            </configuration>
        </plugin>

However, keep in mind that this would only work for maven launched tests and not through the IDE. I haven't found a way for the m2e plugin to understand the requirement to append the javaagent to junit tests.

Eric B.
  • 23,425
  • 50
  • 169
  • 316
0

Try updating you're Maven project. In Eclipse right click your project and go to Maven > Update Projects...

0

My test works when I use JMockit version 1.41, but get the same error in later versions Maven repostitory

Error: java.lang.IllegalStateException: JMockit wasn't properly initialized;

Gryffe
  • 413
  • 1
  • 3
  • 9