title: Dynamically adding JUnit tests to a test class
body: <p>I find myself writing lots and lots of boiler plate tests these days and I want to optimize away a lot of these basic tests in a clean way that can be added to all the current test classes without a lot of hassle.</p>
  
  <p>Here is a basic test class:</p>
  
  <pre><code>class MyClassTest {
  
      @Test
      public void doesWhatItDoes() {
          assertEquals("foo",new MyClass("bar").get());
      }
  
  }
  </code></pre>
  
  <p>Lets say if MyClass implements Serializable, then it stands to reason we want to ensure that it really is serializable. So I built a class which you can extend which contains a battery of standard tests which will be run along side the other tests.</p>
  
  <p>My problem is that if MyClass does NOT implement Serializable for instance, we still have a serialization test in the class. We can make it just succeed for non-serializable classes but it still sticks around in the test list and once this class starts to build it will get more and more cluttered. </p>
  
  <p>What I want to do is find a way to dynamically add those tests which are relevant to already existing test classes where appropriate. I know some of this can be done with a TestSuit but then you have to maintain two test classes per class and that will quickly become a hassle.</p>
  
  <p>If anyone knows of a way to do it which doesn't require an eclipse plug-in or something like that, then I'd be forever grateful.</p>
  
  <p>EDIT: Added a brief sample of what I described above;</p>
  
  <pre><code>class MyClassTest extend AutoTest&lt;MyClass&gt; {
  
      public MyClassTest() {
          super(MyClass.class);
      }
  
      @Test
      public void doesWhatItDoes() {
          assertEquals("foo",new MyClass("bar").get());
      }
  
  }
  
  public abstract class AutoTest&lt;T&gt; {
      private final Class&lt;T&gt; clazz;
      protected AutoTest(Clazz&lt;T&gt; clazz) {
          super();
          this.clazz = clazz;
      }
  
      @Test
      public void serializes() {
          if (Arrays.asList(clazz.getInterfaces()).contains(Serializable.class)) {
          /* Serialize and deserialize and check equals, hashcode and other things... */
          }
      }
  }
  </code></pre>