title: Junit 4 - running all the tests in a test class
body: <p>I have been setting up some contract JUnit 4 tests (following advice in answer to this <a href="https://stackoverflow.com/questions/11157042/java-contract-tests">question</a>) - e.g. if I have an interface (I've included generics in the example, since I'm using them in my project) - but, this is a significantly simplified example:</p>
  
  <pre><code>public interface MyInterface&lt;T&gt; {
    public T myMethod();
  }
  </code></pre>
  
  <p>I can create an abstract contract test - e.g.:</p>
  
  <pre><code>public abstract class AbstractMyInterfaceTest&lt;T&gt; {
  
    private final MyInterface&lt;T&gt; _impl;  
    // plus, store stuff to test against;
  
    public AbstractMyInterfaceTest(MyInterface&lt;T&gt; impl, ... stuff to test against){
      _impl = impl;
      // stuff to test against...
    } 
  
    @Test
    public void testMyMethod(){
      // test some stuff...
    }
  }
  </code></pre>
  
  <p>...and extend this abstract test class for each implementation (which I am running with <code>@RunWith(Parameterized.class)</code>). I'm also passing some variables to test the implementation of the interface against into the abstract test class.</p>
  
  <p>However, in actuality, some interfaces return objects that are implementations of other interfaces, and I would like to be able to test this implementation using the abstract contract class for that interface (...and against the relevant variables). For example, a method in <code>MyInterface</code> might return an Iterator - which is an interface. I have a separate <code>AbstractIteratorTest</code>, which runs a contract test for Iterator against a set of values, and I want to check that the <code>Iterator</code> returned by the implementations of <code>MyInterface</code> passes the contract tests in <code>AbstractIteratorTest</code> with the expected set of values.</p>
  
  <p>At the moment, I'm adding the following to the abstract test classes:</p>
  
  <pre><code>// N.b. not setting @Test - so doesn't run all tests twice...
  public void runTests(){
    testMyMethod();
    // etc.
  }
  </code></pre>
  
  <p>However, this doesn't run each test independently (e.g. running <code>setUp()</code> before each test method), so there are problems if those contract tests expect the implementation to be in a particular state for each test. (A specific example would be testing implementations of the <code>Iterator</code> interface, where each test method can change the state of the tested <code>Iterator</code>)</p>
  
  <p>Is there was a better way of doing this, perhaps making use of features in JUnit 4?</p>
  
  <p>I've seen stuff about the <code>@RunWith(Suite.class)</code> annotation, but that seems to be about running multiple test classes from a single test class, and doesn't allow you to pass variables into the different test classes that are being run. There may be a way to use this  <code>Suite</code> to solve my problem, but I'm not sure what it is...</p>