Last active
August 29, 2015 13:57
-
-
Save jorgevila/9601211 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test.util; | |
import org.junit.Assume; | |
import org.junit.internal.AssumptionViolatedException; | |
import org.junit.rules.MethodRule; | |
import org.junit.runners.model.FrameworkMethod; | |
import org.junit.runners.model.Statement; | |
import java.lang.annotation.ElementType; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
import java.lang.annotation.Target; | |
/** | |
* http://www.codeaffine.com/2013/11/18/a-junit-rule-to-conditionally-ignore-tests/ | |
* http://stackoverflow | |
* .com/questions/10804039/how-to-create-own-annotation-for-junit-that-will-skip- | |
* test-if-concrete-exception | |
* | |
* @author jorgev | |
*/ | |
public class ConditionalIgnoreRule implements MethodRule { | |
public interface IgnoreCondition { | |
boolean isSatisfied(); | |
} | |
@Retention(RetentionPolicy.RUNTIME) | |
@Target({ | |
ElementType.METHOD | |
}) | |
public @interface ConditionalIgnore { | |
Class<? extends IgnoreCondition> condition(); | |
} | |
@Override | |
public Statement apply(Statement base, FrameworkMethod method, Object target) { | |
Statement result = base; | |
if (hasConditionalIgnoreAnnotation(method)) { | |
final IgnoreCondition condition = getIgnoreContition(method); | |
if (condition.isSatisfied()) { | |
result = new IgnoreStatement(condition); | |
} | |
} | |
return result; | |
} | |
private boolean hasConditionalIgnoreAnnotation(FrameworkMethod method) { | |
return method.getAnnotation(ConditionalIgnore.class) != null; | |
} | |
private IgnoreCondition getIgnoreContition(FrameworkMethod method) { | |
final ConditionalIgnore annotation = method.getAnnotation(ConditionalIgnore.class); | |
return newCondition(annotation); | |
} | |
private IgnoreCondition newCondition(ConditionalIgnore annotation) { | |
try { | |
return annotation.condition().newInstance(); | |
} catch (final RuntimeException re) { | |
throw re; | |
} catch (final Exception e) { | |
throw new RuntimeException(e); | |
} | |
} | |
private static class IgnoreStatement extends Statement { | |
private IgnoreCondition condition; | |
IgnoreStatement(IgnoreCondition condition) { | |
this.condition = condition; | |
} | |
@Override | |
public void evaluate() { | |
try { | |
Assume.assumeTrue("Ignored by " + condition.getClass().getSimpleName(), false); | |
} catch (final AssumptionViolatedException e) { | |
System.out.println(e.getMessage()); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test.util; | |
import com.pdi.mca.gvpclient.test.util.ConditionalIgnoreRule.IgnoreCondition; | |
public class NotRunningOnAntBuild implements IgnoreCondition { | |
@Override | |
public boolean isSatisfied() { | |
return !System.getProperty("ant.build").contentEquals("true"); | |
} | |
} | |
/* | |
@Rule | |
public ConditionalIgnoreRule rule = new ConditionalIgnoreRule(); | |
@ConditionalIgnore(condition = NotRunningOnAntBuild.class) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment