TestRule으로이 작업을 수행 할 수 있습니다. TestRule
을 사용하면 테스트 메소드 전후에 코드를 실행할 수 있습니다. 테스트가 예외 (또는 실패한 어설 션에 대한 AssertionError)를 throw하면 테스트가 실패하고 tearDown()을 건너 뛸 수 있습니다. 예제는 다음과 같습니다 그렇지 않으면 두 번 호출되는이 방법에 @After 어노테이션을하지 않도록 당신이 수동의 tearDown을 호출하고
public class ExpectedFailureTest {
public class ConditionalTeardown implements TestRule {
public Statement apply(Statement base, Description description) {
return statement(base, description);
}
private Statement statement(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
try {
base.evaluate();
tearDown();
} catch (Throwable e) {
// no teardown
throw e;
}
}
};
}
}
@Rule
public ConditionalTeardown conditionalTeardown = new ConditionalTeardown();
@Test
public void test1() {
// teardown will get called here
}
@Test
public void test2() {
Object o = null;
o.equals("foo");
// teardown won't get called here
}
public void tearDown() {
System.out.println("tearDown");
}
}
참고. 더 많은 예제를 보려면 ExternalResource.java 및 ExpectedException.java을보십시오.
감사합니다. 그것은 좋은 도움이되었다 .. 정확히 내가 필요로하는 .. – vpradhan