I need to have different sets of parameters so I decided to use @Enclosed. However, the nested classes have to be static and therefore all existing methods/constants called by the methods have to be static. But it is hard because I cannot change the definition of all these methods.
Is there any way to use @Enclosed without adding the definition of existing methods with static?
Here is an example so that you know what I am asking. In the example, since data() is static, class someTest has to be static, so if I call nonStaticMethod(), I got "Cannot make a static reference to the non-static method..." at the line calling nonStaticMethod(). But I do not want to redefine nonStaticMethod() to static.
@RunWith(Enclosed.class)
public class EnclosedParameterizedTest extends EnclosedBase{
@RunWith(Parameterized.class)
public static class SomeTest {
@Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
new Object[]{"NY"},
new Object[]{"CA"},
});
}
String state;
public SomeTest(String state) {
this.state = state;
}
@Test
public void verifyStateTest(){
nonStaticMethod(); //a method already defined in parent of
//EnclosedBase which I cannot re-define
}
}
}
Unfortunately, the Enclosed
runner requires that the enclosed classes be static (aka "nested classes").
Edit: in fact, JUnit will never instantiate the class annotated with @RunWith(Enclosed.class)
. Having a test that uses Enclosed
extend another class doesn't do anything unless the base class has fields or methods annotated with JUnit annotations (@ClassRule
, @BeforeClass
, etc).
I suggest that you share code via delegation, not inheritance. In your case, you can move nonStaticMethod()
to a different class:
@RunWith(Enclosed.class)
public class EnclosedParameterizedTest {
@RunWith(Parameterized.class)
public static class SomeTest {
public final Helper helper = new Helper();
@Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {
new Object[] { "NY" },
new Object[] { "CA" },
});
}
String state;
public SomeTest(String state) {
this.state = state;
}
@Test
public void verifyStateTest(){
helper.nonStaticMethod();
}
}
}
In general, delegation is more flexible than inheritance. One common problem with inheritance in Java is that a class can have only one base class. But another problem is that nested classes can't access the outer class's state, so cannot share code other than static methods.
Edit: If that isn't an option, your nested classes can extend your base class:
@RunWith(Enclosed.class)
public class EnclosedParameterizedTest {
@RunWith(Parameterized.class)
public static class SomeTest extends EnclosedBase {
@Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {
new Object[] { "NY" },
new Object[] { "CA" },
});
}
String state;
public SomeTest(String state) {
this.state = state;
}
@Test
public void verifyStateTest(){
nonStaticMethod();
}
}
}
Alternatively, if you want different sets of parameters for different tests, consider using the JUnitParams runner:
@RunWith(JUnitParamsRunner.class)
public class MyParameterizedTest extends EnclosedBase {
@Parameters({"NY",
"CA" })
@Test
public void verifyStateTest(String state) {
nonStaticMethod();
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments