title: Generic JUnit tests
body: <p>I've got an abstract generic collections class <code>GCollection</code>, and a class that extends that called <code>GStack</code>. </p>
  
  <p>To test the implementation I have an abstract JUnit test class, which I extend for each of the <code>GCollection</code> implementations I do:</p>
  
  <pre><code>public abstract class GCollectionTest&lt;T extends GCollection&lt;E&gt;, E&gt; {
      private GCollection&lt;? extends Object&gt; collection;
      protected abstract GCollection&lt;T&gt; createInstance();
  
      @Before
      public void setup() throws Exception {
          collection = createInstance();
      }
  
      // Tests down here. 
  </code></pre>
  
  <p>This is extended like so:</p>
  
  <pre><code>public class GStackCollectionInterfaceTest&lt;S extends GCollection&lt;E&gt;&gt; {
      protected GDSStack&lt;? extends Object&gt; createInstance() {
          return new GDSStack&lt;String&gt;();
      }
  }
  </code></pre>
  
  <p>I test first with a <code>GStack</code> holding <code>String</code> objects, then re-run the tests with <code>Date</code> objects to ensure it works with different object types. </p>
  
  <pre><code>@Test
  public void testIsEmpty() {
      assertTrue(collection.isEmpty()); // Fresh Stack should hold no objects
      collection.add(new String("Foo")); // Error here.
      assertFalse(collection.isEmpty());
  }
  </code></pre>
  
  <p>The error given is:</p>
  
  <blockquote>
    <p>The method add(capture#24-of ? extends Object) in the type GCollection is not applicable for the arguments (String)</p>
  </blockquote>
  
  <p>My understanding of the error is that I can't put a <code>String</code> object into a <code>GCollection&lt;T extends GCollection&lt;E&gt;&gt;</code> object, but I don't think that's what I'm trying to do. </p>
  
  <h3>What am I doing wrong?</h3>
  
  <p>How can I solve this error while maintaining tests that are as generic as possible? </p>