JUnit Rule
Ruleμ ν
μ€νΈ ν΄λμ€μμ λμ λ°©μμ μ¬μ μ νκ±°λ μ½κ² μΆκ°νλ κ²μ κ°λ₯νκ² ν©λλ€.
μ¬μ©μλ κΈ°μ‘΄μ Ruleμ μ¬μ¬μ©νκ±°λ νμ₯νλ κ²μ΄ κ°λ₯ν©λλ€.
μ΄λ€ Ruleμ΄ μμκΉ?
TemporaryFolder Rule
μμν΄λ, νμΌλ€μ μμ±ν μ μμ΅λλ€.
ν
μ€νΈκ° λͺ¨λ λλ ν μμ ν©λλ€.
κΈ°λ³Έμ μΌλ‘ resourceλ₯Ό μμ νκΈ° λͺ»νλ κ²½μ° μ΄λ ν exceptionλ λ°ννμ§ μμ΅λλ€.
public static class HasTempFolder {
@ Rule
public final TemporaryFolder folder = new TemporaryFolder ();
@ Test
public void testUsingTempFolder () throws IOException {
File createdFile = folder. newFile ( "myfile.txt" );
File createdFolder = folder. newFolder ( "subfolder" );
// ...
}
}
μμ μ₯μμ μ μ₯λλ κ²μ νμΈν μ μμ΅λλ€.
ExternalResources Rule
μΈλΆ Resource(DB Connection, File, Socket) μ΄κΈ°ν / λ°νμ κ΄λ¦¬ν©λλ€.
νΉμ μμμ λ€λ₯Έ ν
μ€νΈ μΌμ΄μ€μμ μ¬μ¬μ©ν λ μ μ©ν©λλ€.
public static class UsesExternalResource {
Server myServer = new Server ();
@ Rule
public final ExternalResource resource = new ExternalResource () {
@ Override
protected void before () throws Throwable {
myServer. connect ();
};
@ Override
protected void after () {
myServer. disconnect ();
};
};
@ Test
public void testFoo () {
new Client (). run (myServer);
}
}
ErrorCollector Rule
μλ¬κ° λ°μνλλΌλ μ§μμ μΌλ‘ ν
μ€νΈλ₯Ό μ§ννκ² λμμ£Όλ Ruleμ
λλ€.
public static class UsesErrorCollectorTwice {
@ Rule
public final ErrorCollector collector = new ErrorCollector ();
@ Test
public void example () {
collector. addError ( new Throwable ( "first thing went wrong" ));
collector. addError ( new Throwable ( "second thing went wrong" ));
}
}
Testλ₯Ό μ§ννλ©΄μ λ°μνλ λͺ¨λ Error κ²°κ³Όλ₯Ό λ°μ λ³Ό μ μμ΅λλ€.
Verifier Rule
ν
μ€νΈ μ체λ₯Ό κ²μ¦νλ assertμλ λ€λ₯΄κ², ν
μ€νΈ μΌμ΄μ€ μ€ν ν λ§μ‘±ν΄μΌνλ ν경쑰건μ΄λ Global쑰건(κ°μ²΄λ€μ μ’
ν© μν)μ κ²μ¬νλλ° μ¬μ©λ©λλ€.
public static class UsesVerifier {
private static String sequence;
@ Rule
public final Verifier collector = new Verifier () {
@ Override
protected void verify () {
sequence += "verify " ;
}
};
@ Test
public void example () {
sequence += "test " ;
}
@ Test
public void verifierRunsAfterTest () {
sequence = "" ;
assertThat ( testResult (UsesVerifier.class), isSuccessful ());
assertEquals ( "test verify " , sequence);
}
}
TestWatcher
ν
μ€νΈ Interceptor (starting, succeeded, failed, finishedβ¦)
AOPμ λΉμ·ν μν μ νλ κ²μΌλ‘ 보μ
λλ€.
import org.junit.AfterClass;
import org.junit.AssumptionViolatedException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import static org.junit.Assert.fail;
public class WatchermanTest {
private static String watchedLog = "" ;
@ Rule
public final TestRule watchman = new TestWatcher () {
@ Override
public Statement apply (Statement base , Description description ) {
return super . apply (base, description);
}
@ Override
protected void succeeded (Description description ) {
watchedLog += description. getDisplayName () + " " + "success! \n " ;
}
@ Override
protected void failed (Throwable e , Description description ) {
watchedLog += description. getDisplayName () + " " + e. getClass (). getSimpleName () + " \n " ;
}
@ Override
protected void skipped (AssumptionViolatedException e , Description description ) {
watchedLog += description. getDisplayName () + " " + e. getClass (). getSimpleName () + " \n " ;
}
@ Override
protected void starting (Description description ) {
super . starting (description);
}
@ Override
protected void finished (Description description ) {
super . finished (description);
}
};
@ AfterClass
public static void teardown (){
System.out. println (watchedLog);
}
@ Test
public void fails () {
fail ();
}
@ Test
public void test_success () {
}
}
ν
μ€νΈ μ 보λ₯Ό λ¨κΈ°λ μ½λλ₯Ό λΆλ¦¬νμ¬ κΈ°λ‘ν μ μμ΅λλ€.
TestName
ν
μ€νΈ λ©μλλͺ
μ μ»μ μ μμ΅λλ€.
public class NameRuleTest {
@ Rule
public final TestName name = new TestName ();
@ Test
public void testA () {
assertEquals ( "testA" , name. getMethodName ());
}
@ Test
public void testB () {
assertEquals ( "testB" , name. getMethodName ());
}
}
Timeout
νλμ ν
μ€νΈκ° ν΅κ³ΌνκΈ° μν timeout μ€μ ν μ μμ΅λλ€. (vs @Timeout)
public static class HasGlobalTimeout {
public static String log;
@ Rule
public final TestRule globalTimeout = Timeout. millis ( 20 );
@ Test
public void testInfiniteLoop1 () {
log += "ran1" ;
for (;;) {}
}
@ Test
public void testInfiniteLoop2 () {
log += "ran2" ;
for (;;) {}
}
}
ExpectedException
μμΈ μ§μ νμΈν μ μμ΅λλ€. (vs @Expected)
Error λ©μμ§λ κ²μ¦μ΄ κ°λ₯ν©λλ€.
public static class HasExpectedException {
@ Rule
public final ExpectedException thrown = ExpectedException. none ();
@ Test
public void throwsNothing () {
}
@ Test
public void throwsNullPointerException () {
thrown. expect (NullPointerException.class);
throw new NullPointerException ();
}
@ Test
public void throwsNullPointerExceptionWithMessage () {
thrown. expect (NullPointerException.class);
thrown. expectMessage ( "happened?" );
thrown. expectMessage ( startsWith ( "What" ));
throw new NullPointerException ( "What happened?" );
}
}
ClassRule
TestSuiteμ ν΄λμ€λ§λ€ μ μ©ν μ μλ Ruleμ
λλ€.
@ RunWith (Suite.class)
@ SuiteClasses ({A.class, B.class, C.class})
public class UsesExternalResource {
public static final Server myServer = new Server ();
@ ClassRule
public static final ExternalResource resource = new ExternalResource () {
@ Override
protected void before () throws Throwable {
myServer. connect ();
};
@ Override
protected void after () {
myServer. disconnect ();
};
};
}
RuleChain
μ¬λ¬κ°μ Rule chainingμΌλ‘ μ μ©ν μ μμ΅λλ€.
public class UseRuleChain {
@ Rule
public final TestRule chain = RuleChain
. outerRule ( new LoggingRule ( "outer rule" ))
. around ( new LoggingRule ( "middle rule" ))
. around ( new LoggingRule ( "inner rule" ));
@ Test
public void example () {
assertTrue ( true );
}
}
Custom Rule
System Rule
Reference