title: How to test different implementations for an interface in Junit5 without duplicating the code
body: <p>May I ask how to write a junit 5 test for an interface with different implementations?</p>
  
  <p>For example, I have a interface <code>Solution</code>, with different implementations like <code>SolutionI</code>, <code>SolutionII</code>, can I write only one test class to test both?</p>
  
  <p>There is a <a href="https://stackoverflow.com/questions/47474695/junit5-test-multiple-classes-without-repeating-code">post</a> shows an example, but if there are multiple test method that needs to be called, I have to pass the parameter for every test method.</p>
  
  <p>May I ask if there is an <strong>elegant</strong> way like what is used in the Junit4</p>
  
  <p>In Junit4, I have a very elegant code sample as follows</p>
  
  <pre><code>@RunWith(Parameterized.class)
  public class SolutionTest {
    private Solution solution;
  
    public SolutionTest(Solution solution) {
      this.solution = solution;
    }
  
    @Parameterized.Parameters
    public static Collection&lt;Object[]&gt; getParameters() {
      return Arrays.asList(new Object[][]{
          {new SolutionI()},
          {new SolutionII()}
      });
    }
    // normal test methods
    @Test
    public void testMethod1() {
  
    }
  }
  </code></pre>
  
  <p>Junit 5 claims <code>ExtendWith()</code> is similar, I tried the following code</p>
  
  <pre><code>@ExtendWith(SolutionTest.SolutionProvider.class)
  public class SolutionTest {
    private Solution solution;
  
    public SolutionTest(Solution solution) {
      System.out.println("Call constructor");
      this.solution = solution;
    }
  
    @Test
    public void testOnlineCase1() {
      assertEquals(19, solution.testMethod(10));
    }
  
    @Test
    public void testOnlineCase2() {
      assertEquals(118, solution.testMethod(100));
    }
  
    static class SolutionProvider implements ParameterResolver {
      private final Solution[] solutions = {
          new SolutionI(),
          new SolutionII()
      };
      private static int i = 0;
  
      @Override
      public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
        return parameterContext.getParameter().getType() == Solution.class;
      }
  
      @Override
      public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
        System.out.println(i);
        return solutions[i++];
      }
    }
  }
  </code></pre>
  
  <p>Unfortunately, <code>testMethod1</code> is using <code>SolutionI</code> and <code>testMethod2</code> is using <code>SolutionII</code>, which makes sense, I don't know if this helps to inspire a little bit.</p>
  
  <p>Thanks for the help in advance</p>