Skip to content

Instantly share code, notes, and snippets.

@acdcjunior
Created December 5, 2013 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acdcjunior/7812740 to your computer and use it in GitHub Desktop.
Save acdcjunior/7812740 to your computer and use it in GitHub Desktop.
JUnit @rule MethodRule example implementation - Exemplo de implementacao de @rule no JUnit
import org.junit.rules.MethodRule;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;
public class ExemploMethodRule implements MethodRule {
@Override
public Statement apply(final Statement base, FrameworkMethod method, Object target) {
System.out.println("ExemploMethodRule#apply() - base: " + base);
System.out.println("ExemploMethodRule#apply() - method (metodo de teste atual): " + method);
System.out.println("ExemploMethodRule#apply() - target (instancia atual da classe de teste): " + target);
return new Statement() {
@Override
public void evaluate() throws Throwable {
System.out.println("->before evaluate()");
try {
base.evaluate();
} finally {
System.out.println("->after evaluate()");
}
}
};
}
}
@acdcjunior
Copy link
Author

Description will be available if you extend TestRule isntead of MethodRule:

private void go(Description description) {
    System.out.println("getDisplayName: "+description.getDisplayName());
    System.out.println("getChildren: "+description.getChildren());
    System.out.println("isSuite: "+description.isSuite());
    System.out.println("isTest: "+description.isTest());
    System.out.println("testCount: "+description.testCount());
    System.out.println("toString: "+description.toString());
    System.out.println("isEmpty: "+description.isEmpty());
    System.out.println("getAnnotations: "+description.getAnnotations());
    System.out.println("getTestClass: "+description.getTestClass());
    System.out.println("getClassName: "+description.getClassName());
    System.out.println("getMethodName: "+description.getMethodName());
    System.out.println("###################################################");
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment