Junit implementation of several runners

I am trying to create a personalized test suite by creating a suiterunner that extends the runner. In the test suite that annotates with @RunWith(suiterunner.class) , I mean the test classes that need to be executed.

Inside the test class, I need to repeat the specific test, for this I use the solution as indicated here: http://codehowtos.blogspot.com/2011/04/run-junit-test-repeatedly.html . but since I created a suiterunner that runs the test class, and inside this test class I implement @RunWith(ExtendedRunner.class) , an initialization error occurs.

I need help managing these 2 runners, and is there also a way to combine 2 runners for a specific test? Is there any other way to solve this problem or any simpler way to continue working?

+7
source share
1 answer

If you use the latest JUnit, you can @Rules become a cleaner solution to your problem. Here is an example:

Imagine this is your application;

 package org.zero.samples.junit; /** * Hello world! * */ public class App { public static void main(String[] args) { System.out.println(new App().getMessage()); } String getMessage() { return "Hello, world!"; } } 

This is your test class;

 package org.zero.samples.junit; import static org.junit.Assert.*; import org.junit.Rule; import org.junit.Test; /** * Unit test for simple App. */ public class AppTest { @Rule public RepeatRule repeatRule = new RepeatRule(3); // Note Rule @Test public void testMessage() { assertEquals("Hello, world!", new App().getMessage()); } } 

Create a rule class, for example:

 package org.zero.samples.junit; import org.junit.rules.TestRule; import org.junit.runner.Description; import org.junit.runners.model.Statement; public class RepeatRule implements TestRule { private int repeatFor; public RepeatRule(int repeatFor) { this.repeatFor = repeatFor; } public Statement apply(final Statement base, Description description) { return new Statement() { @Override public void evaluate() throws Throwable { for (int i = 0; i < repeatFor; i++) { base.evaluate(); } } }; } } 

Perform your test case as usual, just this time your test cases will be repeated for a given number of times. You can find some interesting use cases when @Rule can really be useful. Try to create compound rules, the game around you will surely be glued together.

Hope this helps.

+2
source

All Articles