You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
The text was updated successfully, but these errors were encountered:
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.
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 -
The expected output -
Present error while running -
Version - Junit 4.12
The text was updated successfully, but these errors were encountered: