title: How to test @Before method?
body: <p>JUnit 4.12</p>
  
  <p>I'm currently writing a test for a class methods. Here is how it looks like</p>
  
  <pre><code>public interface MyInterface{
      //method declaration
  }
  
  public class MyClass implements MyInterface{
  
      private int a;
      private in b;
  
      public MyClass(int a, int b){
          if(a &lt;= b + 5)
              throw new IllegalArgumentException("Invalid arguments");
          this.a = a;
          this.b = b;
      }
  
      //methods
  }
  </code></pre>
  
  <p>Now I want to test this class:</p>
  
  <pre><code>public class MyClassTest{
      private static final int THRESHOLD = 1000;
  
      private MyClass mc;
  
      @Before
      public void init(){
          Random rnd = new Random();
          int a = rnd.nexInt(THRESHOLD), 
              b = rnd.nexInt(THRESHOLD);
          mc = new MyClass(a, b);
      }
  }
  </code></pre>
  
  <p>But in this case, <code>init()</code> might throw an exception. So I'd like to test preserving invariants as well as initialize an object in order to test its other methods.</p>
  
  <p>How to do this correctly in JUnit?</p>