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 documentation for better ways to get it initialized.

This is my test implemenation:

package test;

import static mockit.Mockit.*;
import junit.framework.TestCase;
import mockit.*;
import mockit.integration.junit4.*;


import org.junit.*;
import org.junit.runner.*;

import filip.ClassUnderTest;
import filip.LowerClass;

@RunWith(JMockit.class)
public class MockTest extends TestCase {

    @MockClass(realClass = LowerClass.class)
    public static class LowerClassMock {
        @Mock(invocations = 1)
        public String doWork() {
            return "Mockowanie dziala :D";
        }
    }

    @Before
    public void setUp() { setUpMocks(LowerClassMock.class); }

    @After
    public void tearDown() { tearDownMocks(); }

    @Test
    public void testJMockit() {
        ClassUnderTest classUnderTest = new ClassUnderTest();

        classUnderTest.print();
    }

}

Any ideas?

Filip
  • 433
  • 1
  • 5
  • 7

9 Answers9

44

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 {}
Gary
  • 7,167
  • 3
  • 38
  • 57
  • I'm using maven, so I'm trying to use option 2. However, despite ensuring that jmockit is the first dependency in my pom and the surefire plugin is configured to use the -javaagent argLine, I still am getting the "initialized on demand" warning. I'm using surefire 2.6 and jmockit 0.999.4. Any ideas? – Greg Jan 07 '11 at 04:47
  • Perhaps combining with option 3 as well? – Gary Jan 07 '11 at 10:37
  • @GregInYEG, did you ever get it working? I've got the same exact problem. – Ilkka Mar 19 '11 at 16:19
  • @likka Might be worth bumping up the version of jmockit to the latest one which addresses a number of issues like this – Gary Mar 19 '11 at 16:24
  • 1
    Thank you. #3 is what did it for me running under IntelliJ. – Domenic D. Dec 10 '13 at 02:18
  • The option #2 worked like a charm (i.e. without applying options 1 and/or 3). – berezovskyi Jul 18 '14 at 11:36
  • For some reason JMockit suddenly started to froze when I ran my tests in IntelliJ, but thanks to #3 it now works. – Jonathan Jan 13 '16 at 06:15
  • #3 does not work when combined with JUnitParams or any other library that requires the use of RunWith – Novaterata Jul 10 '17 at 16:11
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.

dubi
  • 2,360
  • 2
  • 22
  • 23
Etienne Neveu
  • 12,604
  • 9
  • 36
  • 59
  • 1
    It seems, that using eclipse built-in juint libraries there some problem with JMockit. The resolution is to add external and separate JUnit jar. – Filip May 26 '10 at 08:52
  • 2
    The JMockit installation instructions link in this accepted answer is broken! – fatuhoku Aug 14 '12 at 15:32
  • Had the same problem, point 4 was the right for me. (I had to change the order of my Maven dependencies) – Bevor Oct 20 '12 at 13:51
  • 1
    I had similar problem with Spring Boot. Make sure that you do not have JUnit library included in your build path. I had JUnit lib included in my build path and both JUnit and JMockit dependencies (JMockit preceding JUnit) in my pom.xml. Removing extra JUnit lib from the build path solved my issue. – visrahane Jul 07 '17 at 17:41
  • The pointer to [JMockit installation instructions](http://jmockit.github.io/tutorial/Introduction.html) was very useful The part there to pass a maven argline with javaagent solved my issues right away. No need to have @RunWith(JMockit.class). Impossible when you already have SpringRunner.class using that up. – PCH Jun 29 '20 at 19:23
4

In addition to Gary Rowe's solution:

A more robust (i.e. version and repository path agnostic) integration of JMockit into Surefire would be

<argLine>-javaagent:${org.jmockit:jmockit:jar}

To make this resolution work, the maven-dependency-plugin (version >= 2.5.1!) needs to be configured like this:

<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.5.1</version>
<executions>
    <execution>
        <id>getClasspathFilenames</id>
        <goals>
            <goal>properties</goal>
        </goals>
    </execution>
</executions>

Community
  • 1
  • 1
Peter Wippermann
  • 4,125
  • 5
  • 35
  • 48
1

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

TheJeff
  • 3,665
  • 34
  • 52
  • Upvoted for pointing out that 1.41 is greater than 1.8. It should have really be named 1.08 to avoid confussion. – Paco Abato May 12 '20 at 12:00
0

I simply added:

@RunWith(JMockit.class)

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

Benjamin Slabbert
  • 511
  • 2
  • 9
  • 9
0

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

Ronn Macc
  • 1,271
  • 9
  • 7
0

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.

Peter Lustig
  • 1,585
  • 1
  • 18
  • 34
0

For VSCode JMockito error -

You'll have to make a settings.json file with java.test.config in it.

In that file add :

"java.test.config": [
    {
        "name": "myConfiguration",
        "workingDirectory": "${workspaceFolder}",

        "vmargs": [ 
          "-Xmx512M", //this was here by default
          "-javaagent:/path/to/jmockito/" 
        ]
    ]
Shivansh Jagga
  • 1,541
  • 1
  • 15
  • 24
0

When i configured my maven-surefire-plugin i had two lined with the tag <argLine>...</argLine> and that is what prevented JMockit from initializing. When you debug maven, try to run your lifecycle target with a -e flag so you'll know if the JVM values were set correctly.

RoyalBigMack
  • 620
  • 7
  • 7