Eclipse and JMockit: Method should have no parameters

I am testing out JMockit in Eclipse and I find an error I didn't expect with my very first test. I am using jmockit-1.1, JUnit 4, Java SE 1.7, Juno Service Release 1.

I have the following code as a jUnit test in Eclipse.

package rmb.budget.reader;

import mockit.Mocked;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

public class TestReader {
  public void testReader(
      @Mocked MyReader reader) {
When I run this, I get the below error.
java.lang.Exception: Method testReader should have no parameters
  at org.junit.runners.model.FrameworkMethod.validatePublicVoidNoArg(
  at org.junit.runners.ParentRunner.validatePublicVoidNoArgMethods(
  at org.junit.runners.BlockJUnit4ClassRunner.validateTestMethods(
  at org.junit.runners.BlockJUnit4ClassRunner.validateInstanceMethods(
  at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(
  at org.junit.runners.ParentRunner.validate(
  at org.junit.runners.ParentRunner.(
  at org.junit.runners.BlockJUnit4ClassRunner.(
  at org.junit.runners.JUnit4.(
  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
  at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
  at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
  at java.lang.reflect.Constructor.newInstance(Unknown Source)
  at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(
  at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(
  at org.junit.internal.requests.ClassRequest.getRunner(
  at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.(
  at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.(
  at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(
  at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(

My understanding from the JMockit Getting Started page is that it very definitely should be possible to use (mocked) parameters in @Test methods.

import org.junit.*;
import mockit.*;

public class MyFirstJMockitTest {
   public void testMethodWithMockParameter(
         @Mocked YetAnotherDependency testSpecificMock) {

I raised a bug report but was soon put right by the JMockit developer, Rogerio Liesenfeld. I had two things wrong.

  • In your .classpath file, the JMockit jar must appear before JUnit. For example.
    <classpathentry kind="lib" path="lib/jmockit-1.1.jar"/>
    <classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
  • I was using the vanilla JRE in my project, which fails if you don't use the -javaagent JVM parameter. I found it easier to swap over to a JDK version of Java SE instead (which you can get from the Oracle Java SE download page). I already had it installed - what Java developer wouldn't? (This resulted from having just made my Eclise Java project with default options selected in a newly created workspace.) So now I was using a proper JDK in my project.
    Use a JDK for development in Eclipse, not a vanilla JRE.

Popular Posts