Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parameterized jUnit test without changing runner

Is there a clean way to run parameterized jUnit 4 tests without changing the runner, i.e. without using @RunWith(Parameterized.class)?

I have unit tests which require a special runner already and I can't replace this one with Parameterized. Maybe there is some kind of "runner chaining" so I could both runners at the same time? (Just a wild guess...)

like image 594
Wolfgang Avatar asked Dec 21 '10 13:12

Wolfgang


People also ask

Which JUnit runner class should we use to run the parameterized test?

JUnit 4 has introduced a new feature called parameterized tests. Parameterized tests allow a developer to run the same test over and over again using different values. There are five steps that you need to follow to create a parameterized test. Annotate test class with @RunWith(Parameterized.

Does JUnit support parameterized tests where tests can be executed multiple times with different parameter values?

JUnit 5, the next generation of JUnit, facilitates writing developer tests with shiny new features. One such feature is parameterized tests. This feature enables us to execute a single test method multiple times with different parameters.

How do you pass an object in a parameterized test?

The answer is simple: As any other parameter. Create an array with the parameters you want to supply to a test initiated. Make sure that the test method is provided with the parameters. Execute the test class with the right test runner.


2 Answers

I have released a framework with a couple of runners that are able to enforce parameterization on the test-class while allowing you to chain an arbitrary 3rd-party runner for the actual test-execution.

The framework is CallbackParams - (http://callbackparams.org) - and these are the runners:

  • CallbackParamsRunner
  • BddRunner

By using the framework annotation ...

  • @WrappedRunner

... you can specify an arbitrary 3rd-party runner in this manner:

@RunWith(CallbackParamsRunner.class) // or @RunWith(BddRunner.class)
@WrappedRunner(YourSpecialRunner.class)
public class YourTest {
...

Parameterized tests with CallbackParams differ considerably from the traditional approach to test-parameterization, however. The reasons are explained in this tutorial article with BddRunner explained near the end of the tutorial article.

For your first CallbackParams test you would probably prefer BddRunner, since it requires less boiler-plate stuff, but when you start reusing parameter values between different test-classes you are probably better off with CallbackParamsRunner, which demands stronger type-checking.

Also - with BddRunner you must not have any @Test-methods. Instead you must use the framework annotations @Given, @When and @Then. That requirement sometimes clash with those of the third-party runner but it usually works out quite well.

Good Luck!

like image 188
Henrik Kaipe Avatar answered Sep 28 '22 20:09

Henrik Kaipe


org.junit.runners.Parameterized is created by org.junit.internal.builders.AnnotatedBuilder by reflect mechanism. Maybe you could extend Parameterized as your own Runner: @RunWith(MyParameterized.class).

like image 35
卢声远 Shengyuan Lu Avatar answered Sep 28 '22 20:09

卢声远 Shengyuan Lu