I donโt think there is anything ready, but itโs pretty easy to create a custom annotation for this.
Create your own annotation
@Target( ElementType.METHOD ) @Retention( RetentionPolicy.RUNTIME) public @interface TargetApi { int value(); }
Flip the test runner (which will check the value and ultimately ignore / run the test)
public class ConditionalTestRunner extends BlockJUnit4ClassRunner { public ConditionalTestRunner(Class klass) throws InitializationError { super(klass); } @Override public void runChild(FrameworkMethod method, RunNotifier notifier) { TargetApi condition = method.getAnnotation(TargetApi.class); if(condition.value() > 10) { notifier.fireTestIgnored(describeChild(method)); } else { super.runChild(method, notifier); } } }
and mark your tests
@RunWith(ConditionalTestRunner.class) public class TestClass { @Test @TargetApi(6) public void testMethodThatRunsConditionally() { System.out.print("Test me!"); } }
Just tested, it works for me. :)
Credits for: Conditionally ignoring JUnit tests
source share