10

Without looking into JUnit source itself (my next step) is there an easy way to set the default Runner to be used with every test without having to set @RunWith on every test? We've got a huge pile of unit tests, and I want to be able to add some support across the board without having to change every file.

Ideally I'm hope for something like: -Djunit.runner="com.example.foo".

Gangnus
  • 24,044
  • 16
  • 90
  • 149
Andrew Mellinger
  • 319
  • 3
  • 14
  • 1
    So I peeked into the code where the RunWith annotation is processed and I didn't see anything where it would pull the default runner from somewhere else. – Andrew Mellinger May 05 '11 at 13:29

3 Answers3

2

I don't think this is possible to define globally, but if writing you own main function is an option, you can do something similar through code. You can create a custom RunnerBuilder and pass it to a Suite together with your test classes.

Class<?>[] testClasses = { TestFoo.class, TestBar.class, ... };
RunnerBuilder runnerBuilder = new RunnerBuilder() {
    @Override
    public Runner runnerForClass(Class<?> testClass) throws Throwable {
        return new MyCustomRunner(testClass);
    }
};
new JUnitCore().run(new Suite(runnerBuilder, testClasses));

This won't integrate with UI test runners like the one in Eclipse, but for some automated testing scenarios it could be an option.

Soulman
  • 2,910
  • 24
  • 21
1

JUnit doesn’t supporting setting the runner globally. You can hide away the @RunWith in a base class, but this probably won't help in your situation.

Peter Niederwieser
  • 121,412
  • 21
  • 324
  • 259
0

Depending on what you want to achieve, you might be able to influence the test behavior globally by using a custom RunListener. Here is how to configure it with the Maven Surefire plugin: http://maven.apache.org/plugins/maven-surefire-plugin/examples/junit.html#Using_custom_listeners_and_reporters

Madoc
  • 5,841
  • 4
  • 25
  • 38