To run tests that use any of the JMockit APIs, use your Java IDE, Ant/Maven script, etc. the way you normally would.
The only additional requirement is that the JVM gets a chance to load the "Java agent" which provides instrumentation
access to JMockit (it's all inside jmockit.jar).
There are a few different ways to satisfy this requirement, depending on whether JDK 1.5 or 1.6+
is used to run the tests, and which test framework is used (JUnit or TestNG):
jmockit.jar appears before the
JUnit jar in the classpath.
Only JUnit 4.5 or newer is supported (test classes which extend
junit.framework.TestCase are also supported).
jmockit.jar (version 0.999.11+) to the
classpath.
For older versions of TestNG (5.14 to 6.1) or JMockit, the
mockit.integration.testng.Initializer class must be configured as a TestNG
listener.
This can be done by passing "-listener mockit.integration.testng.Initializer" as a test runner
argument in the JVM command line;
when using Maven, the listener can be specified by configuring the maven-surefire-plugin with a
property of name "listener" and a value containing "mockit.integration.testng.Initializer" (see
this page for more
details);
another way is to specify the listener in the testng.xml configuration file:
<listener class-name="mockit.integration.testng.Initializer"/>.
-javaagent:jmockit.jar as a JVM initialization parameter.
This can easily be done in the "Run/Debug Configuration" for both Eclipse and IntelliJ IDEA.
(Note that if jmockit.jar is not in the current working directory when running the tests, you will have
to insert the appropriate absolute or relative path, so that the JVM can locate the jar file.)
When using the <junit> element in a build.xml script, it's important to use a separate
JVM instance. For example, something like the following:
<junit fork="yes" forkmode="once" dir="directoryContainingJars">
<!-- If needed (eg, when running on JDK 1.5), uncomment the following JVM argument:
<jvmarg value="-javaagent:jmockit.jar"/> -->
<classpath path="jmockit.jar"/>
<!-- To generate (if desired) a code coverage HTML report: -->
<classpath path="jmockit-coverage.jar"/>
<!-- additional classpath entries, including one for the appropriate junit.jar -->
<batchtest>
<!-- filesets specifying the desired test classes -->
</batchtest>
</junit>
For a complete example, see the "sampleTests" target in the build.xml file
found inside the full distribution.
The JMockit artifacts for releases 0.999.11 and newer are located in the central Maven repository. Releases 0.999.5 to 0.999.10, however, are not in the central repository, but in JMockit's SVN repository. For these older versions, therefore, you will need to add the following repository to your Maven configuration:
<!-- Only for versions older than 0.999.11 -->
<repository>
<id>jmockit-svn</id>
<url>http://jmockit.googlecode.com/svn/maven-repo</url>
<releases><checksumPolicy>ignore</checksumPolicy></releases>
</repository>
For any project containing JMockit tests, add the following dependency to the pom.xml file:
<dependency>
<groupId>com.googlecode.jmockit</groupId> <!-- "mockit" for versions before 0.999.11 -->
<artifactId>jmockit</artifactId>
<version>0.999.15</version>
<scope>test</scope>
</dependency>
This dependency should come before the junit dependency, if any.
To run tests with JDK 1.5, you will need to make sure the JVM is started with the -javaagent
initialization parameter. This can be done in the Maven Surefire plug-in as follows:
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>
-javaagent:"${settings.localRepository}"/com/googlecode/jmockit/jmockit/0.999.15/jmockit-0.999.15.jar
</argLine>
</configuration>
</plugin>
Note that the above JVM parameter is not required when running tests on a JDK 1.6+ environment which contains a supported implementation of the Attach API (ie, a HotSpot or JRockit JVM on Windows or Linux). Even if you have to deploy production code in a JDK 1.5 environment, consider using JDK 1.6 in your local development environment; with the proper build/IDE configuration, a JDK 1.6 Java compiler (either "javac" or the Eclipse compiler) can safely be used to compile Java 1.5 code while generating Java 1.5-compliant class files.
For information on using JMockit Coverage with Maven, see the relevant section in the JMockit Tutorial.