Parameterized jUnit test without changing runner
Asked Answered
L

2

10

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

Limb answered 21/12, 2010 at 13:1 Comment(0)
M
1

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

Mikvah answered 24/12, 2010 at 5:37 Comment(1)
Thanks, good idea! Well, the special runner I need is not written by me and I have to use it as is. But I can apply your solution the other way around: I'll extend the special runner such that it has the same functionality as Parameterized. Let's see if JUnit's license allows some copy and paste. ;-)Limb
M
6

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!

Multiplex answered 9/4, 2011 at 8:42 Comment(0)
M
1

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

Mikvah answered 24/12, 2010 at 5:37 Comment(1)
Thanks, good idea! Well, the special runner I need is not written by me and I have to use it as is. But I can apply your solution the other way around: I'll extend the special runner such that it has the same functionality as Parameterized. Let's see if JUnit's license allows some copy and paste. ;-)Limb

© 2022 - 2024 — McMap. All rights reserved.