package com.mycompany.sandbox;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
public class RuleExamTest {
@Rule
public TestRule myTestRule = new TestRule() {
@Override
public Statement apply(final Statement base, final Description desc) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
System.out.println("RULE PRE PROCESS");
base.evaluate();
System.out.println("RULE POST PROCESS");
}
};
}
};
@Before
public void setUp() {
System.out.println("BEFORE");
}
@After
public void tearDown() {
System.out.println("AFTER");
}
@Test
public void test1() {
System.out.println("TEST1");
}
@Test
public void test2() {
System.out.println("TEST2");
}
}
------------- Standard Output --------------- RULE PRE PROCESS BEFORE TEST1 AFTER RULE POST PROCESS RULE PRE PROCESS BEFORE TEST2 AFTER RULE POST PROCESS ------------- ---------------- ---------------
http://javasourcecode.org/html/open-source/junit/junit-4.10/org/junit/rules/TestRule.html
http://stefanbirkner.github.com/system-rules/
package com.mycompany.sandbox;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExternalResource;
import org.junit.rules.RuleChain;
import org.junit.rules.TestRule;
public class RuleChainExamTest {
@Rule
public TestRule chain
= RuleChain
.outerRule(new MyRule("1"))
.around(new MyRule("2"));
@Test
public void test() {
System.out.println("TEST");
}
class MyRule extends ExternalResource {
private String pId;
public MyRule(String id) {
pId = id;
}
@Override
public void before() throws Exception {
System.out.println("BEFORE " + pId);
}
@Override
public void after() {
System.out.println("AFTER" + pId);
}
}
}
------------- Standard Output --------------- BEFORE 1 BEFORE 2 TEST AFTER2 AFTER1 ------------- ---------------- ---------------
package com.mycompany.sandbox;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExternalResource;
import org.junit.rules.TestRule;
public class ClassRuleExamTest {
@ClassRule
public static TestRule myTestClassRule = new ExternalResource() {
@Override
public void before() throws Exception {
System.out.println("CLASS RULE BEFORE");
}
@Override
public void after() {
System.out.println("CLASS RULE AFTER");
}
};
@Rule
public TestRule myTestRule = new ExternalResource() {
@Override
public void before() throws Exception {
System.out.println("RULE BEFORE");
}
@Override
public void after() {
System.out.println("RULE AFTER");
}
};
@BeforeClass
public static void setUpClass() {
System.out.println("CLASS BEFORE");
}
@AfterClass
public static void tearDownClass() {
System.out.println("CLASS AFTER");
}
@Before
public void setUp() {
System.out.println("BEFORE");
}
@After
public void tearDown() {
System.out.println("AFTER");
}
@Test
public void test1() {
System.out.println("TEST1");
}
@Test
public void test2() {
System.out.println("TEST2");
}
}
------------- Standard Output --------------- CLASS RULE BEFORE CLASS BEFORE RULE BEFORE BEFORE TEST1 AFTER RULE AFTER RULE BEFORE BEFORE TEST2 AFTER RULE AFTER CLASS AFTER CLASS RULE AFTER ------------- ---------------- ---------------
package com.mycompany.sandbox;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface CI {
public enum DO {EXEC, IGNORE};
public DO value();
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.mycompany.sandbox;
import com.mycompany.sandbox.CI.DO;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
/**
*
* @author atsushi
*/
public class RuleDescTest {
@Rule
public TestRule myTestRule = new TestRule() {
@Override
public Statement apply(final Statement base, final Description desc) {
System.out.println(desc.getTestClass().getName() + "#" + desc.getMethodName());
CI ci = desc.getAnnotation(CI.class);
System.out.println("IF THIS RUN ON CI:" + ci.value());
return new Statement() {
@Override
public void evaluate() throws Throwable {
base.evaluate();
}
};
}
};
@Test
@CI(DO.EXEC)
public void test() {
System.out.println("TEST");
}
}
------------- Standard Output --------------- com.mycompany.sandbox.RuleDescTest#test IF THIS RUN ON CI:EXEC TEST ------------- ---------------- ---------------