Home | History | Annotate | Download | only in statements
      1 package org.junit.internal.runners.statements;
      2 
      3 import org.junit.internal.AssumptionViolatedException;
      4 import org.junit.runners.model.Statement;
      5 
      6 public class ExpectException extends Statement {
      7     private final Statement next;
      8     private final Class<? extends Throwable> expected;
      9 
     10     public ExpectException(Statement next, Class<? extends Throwable> expected) {
     11         this.next = next;
     12         this.expected = expected;
     13     }
     14 
     15     @Override
     16     public void evaluate() throws Exception {
     17         boolean complete = false;
     18         try {
     19             next.evaluate();
     20             complete = true;
     21         } catch (AssumptionViolatedException e) {
     22             throw e;
     23         } catch (Throwable e) {
     24             if (!expected.isAssignableFrom(e.getClass())) {
     25                 String message = "Unexpected exception, expected<"
     26                         + expected.getName() + "> but was<"
     27                         + e.getClass().getName() + ">";
     28                 throw new Exception(message, e);
     29             }
     30         }
     31         if (complete) {
     32             throw new AssertionError("Expected exception: "
     33                     + expected.getName());
     34         }
     35     }
     36 }