Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Config based enable/disable JUnit Test

Tags:

java

junit

What is the best way to enable/disable a junit test based on a Config parameter? Say I have a Config class which dictates some state of the software that makes a given set of tests invalid.

I could put the body of the test in an if statement within the test method, e.g.:

@Test
public void someTest() {
   if(Config.shouldIRunTheTests()) {
      //do the actual test
   }
}

This seems bad because I actually get test passes for these cases when I actually want these tests skipped. Would like something like:

@Test[Config.shouldIRunTheTests()] 
public void someTest() {
    //do the actual test
}

Is this possible?

like image 846
Ryan R. Avatar asked Oct 24 '25 19:10

Ryan R.


2 Answers

Actually I think the best solution on this case is to write your own org.junit.Runner. It is not so complicated as it seems. A simple sample would be:

The Runner:

package foo.bar.test;

import org.junit.runner.Description;
import org.junit.runner.Runner;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.JUnit4;
import org.junit.runners.model.InitializationError;

public class MyRunner extends Runner {

    private final Runner runner;

    public MyRunner(final Class<?> klass) throws InitializationError {
        super();
        this.runner = new JUnit4(klass);
    }

    @Override
    public Description getDescription() {
        return runner.getDescription();
    }

    @Override
    public void run(final RunNotifier notifier) {
        for (Description description : runner.getDescription().getChildren()) {
            notifier.fireTestStarted(description);
            try {
                // here it is possible to get annotation:
                // description.getAnnotation(annotationType)
                if (MyConfiguration.shallExecute(description.getClassName(), description.getMethodName())) {
                    runner.run(notifier);
                }
            } catch (Exception e) {
                notifier.fireTestFailure(new Failure(description, e));
            }
        }
    }

}

The test case:

package foo.bar.test;

import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(MyRunner.class)
public class TestCase {

    @Test
    public void myTest() {
        System.out.println("executed");
    }

}

And configuration class:

package foo.bar.test;

public class MyConfiguration {

    public static boolean shallExecute(final String className, final String methodName) {
        // your configuration logic
        System.out.println(className + "." + methodName);
        return false;
    }

}

Here the cool thing is that you could implement your own annotation, for example: @TestKey("testWithDataBase"), see comments on the example source above. And your configuration object could define if the test should run or not, so you can group tests, what is quite useful when you have a lot of tests that needs to be grouped.

like image 188
Francisco Spaeth Avatar answered Oct 26 '25 07:10

Francisco Spaeth


Actually, looks like I found this with google: http://old.nabble.com/How-to-enable-disable-JUnit-tests-programatically---td23732375.html

EDIT:

This worked for me:

@Test 
public void someTest() {
   org.junit.Assume.assumeTrue(Config.shouldIRunTheTests());
   //do the actual test
}
like image 44
Ryan R. Avatar answered Oct 26 '25 09:10

Ryan R.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!