JUnit4 - Parameterized test extends parameterized base class - need Cartesian product of them

886 views Asked by At

MyBaseIntegrationTestCase is a class, which is a parent for ~ 150 test classes. Now, I want to make MyBaseIntegrationTestCase parameterized (parameter is version of external software)

So, I change it to smth like

@RunWith(Parameterized.class)
public abstract class MyBaseIntegrationTestCase extends MyBaseTestCase{

 @Parameterized.Parameters(name = "with ExternalSoftware-v{0}")
  public static List<String[]> allVersions() {
    return Arrays.asList( ...);
  }

  @Parameterized.Parameter
  public String externalSoftwareVersion;
  
}

All children of MyBaseIntegrationTestCase became parameterized and run successfully, except the MyTestClass, declared as

@RunWith(Parameterized::class)
class MyTestClass(override var myType: ParameterType) : MyBaseIntegrationTestCase() {
  companion object {
    @Parameterized.Parameters(name = "with {0}")
    @JvmStatic
    fun allTypes(): Collection<Array<ParameterType>> = ...
}

it fails with

java.lang.Exception: Test class should have exactly one public zero-argument constructor

I want to make all children of MyBaseIntegrationTestCase run parameterized as defined in parent class, and make MyTestClass parameterized on Cartesian product of allType and allVersions

It is impossible to make MyTestClass not to extend MyBaseIntegrationTestCase, this will require enormous amount of stuff to be copy-pasted.

Could you advice any ideas how to do it in JUnit4?

1

There are 1 answers

0
Alexander Bubenchikov On

if anyone interested classes should be declared as:

@RunWith(Parameterized.class)
public abstract class MyBaseIntegrationTestCase extends MyBaseTestCase{

 @Parameterized.Parameters(name = "with ExternalSoftware-v{0}")
  public static List<String[]> allVersions() {
    return Arrays.asList( ...);
  }

  @Parameterized.Parameter(0) // important to set index explicitly
  public String externalSoftwareVersion;
  
}

and

@RunWith(Parameterized::class)
class MyTestClass : MyBaseIntegrationTestCase() {
  @Parameterized.Parameter(1) // important to set index explicitly
  var myType: ParameterType
  companion object {
    @Parameterized.Parameters(name = "with {1} on version {0}")
    @JvmStatic
    fun allTypes(): Collection<Array<Any>> = ... //cartesian product should be here
}