Globally for JUnit runner instead of @RunWith

Without looking at the JUnit source (my next step), is there an easy way to set the default Runner for each test without having to install @RunWith on each test? We have a huge bunch of unit tests, and I want to be able to add some support in all directions without changing every file.

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

+6
source share
3 answers

I don’t think it can be defined globally, but if you write your own main function, you can do something similar with code. You can create a custom RunnerBuilder and transfer it to the Suite along 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 will not integrate with UI testing runners, as in Eclipse, but for some automated testing scenarios this might be an option.

+1
source

JUnit does not support runner customization worldwide. You can hide @RunWith in the base class, but that probably won't help in your situation.

+1
source

Depending on what you want to achieve, you can influence the behavior of the test worldwide using a custom RunListener . Here's how to configure it using the Maven Surefire plugin: http://maven.apache.org/plugins/maven-surefire-plugin/examples/junit.html#Using_custom_listeners_and_reporters

0
source

Source: https://habr.com/ru/post/887292/


All Articles