Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Junit Parameterized tests together with Powermock - how?

I've been trying to figure out how to run parameterized tests in Junit4 together with PowerMock. The problem is that to use PowerMock you need to decorate your test class with

@RunWith(PowerMockRunner.class) 

and to use parameterized tests you have to decorate with

@RunWith(Parameterized.class) 

From what I can see they seem mutually excluded!? Is this true? Is there any way around this? I've tried to create a parameterized class within a class running with PowerMock; something like this:

@RunWith(PowerMockRunner.class) class MyTestClass {      @RunWith(Parameterized.class)      class ParamTestClass {           // Yadayada      } } 

But unfortunately this doesn't do much good... The ParamTestClass still doesn't run with PowerMock support (not that surprisingly maybe)... And I've kind of run out of ideas so any help is greatly appreciated!

Update: For future googlers also see: Using PowerMock without the RunWith?

like image 454
Anders Hansson Avatar asked Mar 16 '10 13:03

Anders Hansson


People also ask

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.

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.


2 Answers

I had the same issue. Unfortunately it would not let me use a PowerMock Rule due to the JVM I had. Instead of the rule I used RunnerDelegate.

@RunWith(PowerMockRunner.class) @PowerMockRunnerDelegate(Parameterized.class) 
like image 135
Abercrombieande Avatar answered Oct 08 '22 17:10

Abercrombieande


Yes this works by using the PowerMock Rule available if you use JUnit 4.7+.

like image 24
Johan Avatar answered Oct 08 '22 17:10

Johan