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?
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:
The test case:
And configuration class:
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.