Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cartesian product of base and derived class parameter #1600

Open
noviceamateur opened this issue Mar 27, 2019 · 3 comments
Open

Cartesian product of base and derived class parameter #1600

noviceamateur opened this issue Mar 27, 2019 · 3 comments

Comments

@noviceamateur
Copy link

noviceamateur commented Mar 27, 2019

I have parameters in both the base and derived test classes. So I want the tests in derived class to run for the cartesian product of those.

Sample code -

@RunWith(Parameterized.class)
public class BaseTest {

  public BaseTest() {
  }

  @BeforeClass
  public static void setupbase() {
  }

  @Parameterized.Parameter(0)
  public String baseParam;

  @Parameterized.Parameters
  public static Collection<Object[]> data() {
    return Arrays.asList(new Object[][] {
        { "base1" }, { "base2" }
    });
  }
}



public class Test1 extends BaseTest {

  @BeforeClass
  public static void setup() {
  }

  @Parameterized.Parameter(0)
  public  String derivedParam;

  @Parameterized.Parameters
  public static Collection<Object[]> data() {
    return Arrays.asList(new Object[][] {
        { "derived1" }, { "derived2" }
    });
  }

  @Test
  public void testcase1() {
    System.out.println("Base Param = " + baseParam);
    System.out.println("Derived Param = " + derivedParam);
  }
}

The expected output -

Base Param = base1
Derived Param = derived1
Base Param = base1
Derived Param = derived2
Base Param = base2
Derived Param = derived1
Base Param = base2
Derived Param = derived2

Present error while running -

> java.lang.Exception: @Parameter(0) is used more than once (2).
> 
> 	at org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParameters.validateFields(BlockJUnit4ClassRunnerWithParameters.java:119)
> 	at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:129)
> 	at org.junit.runners.ParentRunner.validate(ParentRunner.java:416)
> 	at org.junit.runners.ParentRunner.<init>(ParentRunner.java:84)
> 	at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:65)
> 	at org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParameters.<init>(BlockJUnit4ClassRunnerWithParameters.java:27)
> 	at org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParametersFactory.createRunnerForTestWithParameters(BlockJUnit4ClassRunnerWithParametersFactory.java:16)
> 	at org.junit.runners.Parameterized.createRunnersForParameters(Parameterized.java:313)
> 	at org.junit.runners.Parameterized.<init>(Parameterized.java:248)
> 	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
> 	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
> 	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
> 	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
> 	at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
> 	at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
> 	at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
> 	at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
> 	at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
> 	at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
> 	at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:36)
> 	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:49)
> 	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
> 	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
> 	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
> 
> 
> java.lang.Exception: @Parameter(1) is never used.
> 
> 	at org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParameters.validateFields(BlockJUnit4ClassRunnerWithParameters.java:116)
> 	at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:129)
> 	at org.junit.runners.ParentRunner.validate(ParentRunner.java:416)
> 	at org.junit.runners.ParentRunner.<init>(ParentRunner.java:84)
> 	at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:65)
> 	at org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParameters.<init>(BlockJUnit4ClassRunnerWithParameters.java:27)
> 	at org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParametersFactory.createRunnerForTestWithParameters(BlockJUnit4ClassRunnerWithParametersFactory.java:16)
> 	at org.junit.runners.Parameterized.createRunnersForParameters(Parameterized.java:313)
> 	at org.junit.runners.Parameterized.<init>(Parameterized.java:248)
> 	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
> 	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
> 	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
> 	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
> 	at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
> 	at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
> 	at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
> 	at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
> 	at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
> 	at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
> 	at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:36)

Version - Junit 4.12

@marcphilipp
Copy link
Member

I'm afraid that's not how Parameterized works. It will only find one of the @Parameters methods. Thus, you'll need to override the method in the subclass and compute the cartesian product yourself.

@noviceamateur
Copy link
Author

Override which method in my subclass ?

Also, could this be taken up as a feature request in that case ?
Is it possible to do so in Junit5 ?

@kcooney
Copy link
Member

kcooney commented Mar 27, 2019

Have you considered the Theories runner?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants