Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JMockit - initialization problem

Tags:

java

jmockit

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?

like image 460
Filip Avatar asked May 25 '10 14:05

Filip


4 Answers

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 {} 
like image 122
Gary Rowe Avatar answered Sep 20 '22 06:09

Gary Rowe


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.

like image 22
Etienne Neveu Avatar answered Sep 21 '22 06:09

Etienne Neveu


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>

like image 45
Peter Wippermann Avatar answered Sep 21 '22 06:09

Peter Wippermann


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

like image 33
TheJeff Avatar answered Sep 19 '22 06:09

TheJeff