Search code examples
eclipsejunitjmockit

How do I run a JUnit class annotated with @RunWith on Eclipse?


Usually, I would run my test classes by right clicking the class -> Run As -> JUnit Test. But I get errors if the class is annotated with @RunWith

For example, for a test class with the following structure:

import org.junit.Test;
import org.junit.runner.RunWith;
import mockit.integration.junit4.JMockit;

@RunWith(JMockit.class)
public class SwiftResourceIT {

}

I get the following error:

Caused by: java.lang.IllegalStateException: Running on JDK 9 requires -javaagent:<proper path>/jmockit-1.n.jar or -Djdk.attach.allowAttachSelf

As a Maven project that is configured to run tests when I do a build, the tests run fine when I clean install. I just don't know how to run this class by itself.

Thanks in advance


Solution

  • This is something you can configure in the "Run Configuration". Right after failing to run the test class, bring up the "Run Configurations" dialog (multiple ways to get there), the one you just ran should be selected. Click on the "Arguments" tab, put the required "javaagent" command-line arguments into that field and store them.

    If you don't like having to edit this for every test class, you might consider changing your mocking technology. Mockito has no need for something like this.