Декартово произведение базовых и производных параметров класса в junit4 - PullRequest
0 голосов
/ 27 марта 2019

У меня есть параметры как в базовых, так и в производных тестовых классах.Поэтому я хочу, чтобы тесты в производном классе выполнялись для декартовых произведений этих классов.Есть ли способ сделать это в junit4?

Пример кода -

@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);
  }
}

Ожидаемый вывод -

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

Присутствует ошибка во время работы -

> 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)

Версия - Junit 4.12

Я также открыт для использования JunitParams.

...