ä»çµã¿ã¨ã説æããã®ãé¢åã ããã½ã¼ã¹ãåºãã®ãæéã ããè«ãã証æ ã£ã¦ä¾¿å©ãªè¨èã«ä¹ã£ãã£ã¦ã
ãããªãã¹ãããã£ãã¨ãããããããªããã©ããªããã©ãã¾ããã£ãã¨ããã
@Grab('junit:junit:4.11') import org.junit.* import org.junit.rules.* import org.junit.runner.* import org.junit.runners.model.* class Tests { static { println 'static-initializer' } @ClassRule public static TestRule classRule = {s,d ->{-> println 'ClassRule-before'; s.evaluate(); println 'ClassRule-after' } as Statement} @BeforeClass static void setUpClass() { println 'BeforeClass' } { println 'initializer' } Tests() { println '<init>' } @Rule public TestRule rule = {s,d ->{-> println 'Rule-before'; s.evaluate(); println 'Rule-after' } as Statement} @Before void setUp() { println 'Before' } @Test void testMethod1() { println 'test1' } @Test void testMethod2() { println 'test2' } @After void tearDown() { println 'After' } @AfterClass static void tearDownClass() { println 'AfterClass' } }
ã§å®è¡ãããã¨ãããåºãã
static-initializer ClassRule-before BeforeClass initializer <init> Rule-before Before test1 After Rule-after initializer <init> Rule-before Before test2 After Rule-after AfterClass ClassRule-after JUnit 4 Runner, Tests: 2, Failures: 0, Time: 31
ãã¼ãã¼ï¼
- staticã¤ãã·ã£ã©ã¤ã¶
- @ClassRule ã®åå¦ç
- @BeforeClass
- ï¼ãã¹ãã¡ã½ããæ°ç¹°ãè¿ãï¼
- ã¤ã³ã¹ã¿ã³ã¹ã¤ãã·ã£ã©ã¤ã¶
- ã³ã³ã¹ãã©ã¯ã¿
- @Rule ã®åå¦ç
- @Before
- *ãã¹ãã¡ã½ããæ¬ä½*
- @After
- @Rule ã®å¾å¦ç
- @AfterClass
- @ClassRule ã®å¾å¦ç
JUnitã®ã³ã¼ããèªãã§ãããäºæ³åºæ¥ã人ã«ã¯ãããã¾ãã®è©±ããªãå¥æ®µJUnitã ããã¨ãé¢ä¿ãªã話ãæ··ãã£ã¦ããã©ã
ãã¹ãã¡ã½ããã§ã¤ãã·ã£ã©ã¤ã¶ã¨ãã³ã³ã¹ãã©ã¯ã¿ã¨ã使ããã¨ã¯ï¼Parameterizedã©ã³ãã¼ã使ç¨ããå ´åãªã©ãé¤ãã¦ï¼ç¡ãã¨ã¯æãã¾ãããã¨ãããããããªæãã ãã¨ã
Ruleã¨Before/Afterã¨ã®é çªã¯æ©ããã¨ãæãã®ããªã¼ã¨æããªããç¡ãã®ã§ããããããªæ©ã¿ãæ±ãã¦èª¿ã¹ããããããããªããç´ ç´ã«RuleChainããã¨ãã使ããã¨ãä»ã¯ãããªã£ã¦ãã£ã¦ã ãã§ãä»å¾ã®ãã¼ã¸ã§ã³ã§Ruleã¨Before/Afterã®é çªãå¤ãã£ã¦ãæå¥ã¯ããã¾ãããç¹ã«ããã¥ã¡ã³ãåãããã¦ãªãã£ãã¯ãã ãã