====== JUnit Rules ====== * [[https://github.com/junit-team/junit/wiki/Rules|JUnit Rules]] * [[http://www.threeriversinstitute.org/blog/?p=155|Interceptors in JUnit]] * [[http://java.dzone.com/articles/junit-rules-0|JUnit Rules]] * [[http://cwd.dhemery.com/2010/12/junit-rules/|Using Rules to Influence JUnit Test Execution]] * [[http://www.alexecollins.com/tutorial-junit-rule/|Alex Collins - Tutorial: JUnit @Rule]] * [[http://blog.schauderhaft.de/2009/10/04/junit-rules/|JUnit new feature Rules]] ===== Exception Rule ===== * 예외 발생 여부와 발생한 예외 종류를 검증할 수 있는 Rule @Rule public ExpectedException exception = ExpectedException.none(); @Test public void throwsIllegalArgumentExceptionIfIconIsNull() { // 발생할 예외에 대한 조건 기술 exception.expect(IllegalArgumentException.class); exception.expectMessage("Negative value not allowed"); ClassToBeTested t = new ClassToBeTested(); t.methodToBeTest(-1); } ===== 임시 폴더 Rule ===== * 테스트 별로 임시 폴더를 생성한다. @Rule public TemporaryFolder folder = new TemporaryFolder(); // ... File createdFolder = folder.newFolder("newfolder"); File createdFile = folder.newFile("myfilefile.txt"); ===== External Resource Rule ===== * 외부 리소스 초기화/종료를 처리하는 룰. * 대부분의 Custom Rule은 External Resource Rule을 상속 받아 만들어도 충분한 경우가 많다. public static class UsesExternalResource { Server myServer = new Server(); @Rule public 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); } } ===== System Rules ===== * http://stefanbirkner.github.io/system-rules/