Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Globally setting a JUnit runner instead of @RunWith

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".

like image 655
Andrew Mellinger Avatar asked May 04 '11 15:05

Andrew Mellinger


2 Answers

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.

like image 64
Soulman Avatar answered Oct 03 '22 15:10

Soulman


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.

like image 26
Peter Niederwieser Avatar answered Oct 03 '22 13:10

Peter Niederwieser