问题描述
我正在使用junit 4的参数化"功能,并且我注意到@parameters方法在@beforeclass方法之前执行.这给我造成了一个问题,因为我通过@parameters传递给测试用例的参数取决于@beforeclass方法中初始化的代码.例如
I'm using "parametrized" feature of junit 4 and I noticed that @parameters method is executed before @beforeclass method. This is creating a problem for me because the parameters i'm passing to the test cases via @parameters depends on the code initialize in the @beforeclass method. For example
@RunWith(Parameterized.class)
public class TestOtherClass {
String argument;
private static boolean initializeThis;
public TestOtherClass(String parameter) throws Exception {
argument=parameter;
}
@BeforeClass
public static void doSetup() {
System.out.println("Doing setup before class...");
initializeThis=true; // true or false, based on some condition
}
@Test
public void otherTest() {
System.out.println("Other test: " + argument);
}
@Parameters
public static Collection<Object[]> getData(){
System.out.println("Inside parameter");
String addThis;
if(initializeThis)
addThis="adding true";
else
addThis="adding false";
Object[] para1 = new Object[]{"First parameter :: " + addThis};
Object[] para2 = new Object[]{"Second parameter :: " + addThis};
Collection<Object[]> classNames = new ArrayList<Object[]>();
classNames.add(para1);
classNames.add(para2);
return classNames;
}
}
现在,我正在@beforeclass方法中将变量"initializeThis"初始化为true,但是(令人惊讶地)当我执行打印的测试用例时
Now, I'm initializing variable "initializeThis" to true in @beforeclass method but (surprisingly) when I executed the test case it prints
Other test: First parameter :: adding false
Other test: Second parameter :: adding false
那是意料之外的事情.
我的问题是;有什么方法可以在@parameters之前执行@beforeclass方法,我们可以在junit 4中执行此操作吗?
That is something not expected.
My question is; is there any way to execute the @beforeclass method before @parameters, can we do this is in junit 4?
推荐答案
我将只使用普通的旧Java静态{..}初始化程序,而不使用@BeforeClass,例如:
I would use just plain old java static {..} initializer instead of @BeforeClass, e.g.:
@RunWith(Parameterized.class)
public class TestOtherClass {
String argument;
private static boolean initializeThis;
public TestOtherClass(String parameter) throws Exception {
argument=parameter;
}
static {
doSetup();
}
// @BeforeClass
public static void doSetup() {
System.out.println("Doing setup before class...");
initializeThis=true; // true or false, based on some condition
}
@Test
public void otherTest() {
System.out.println("Other test: " + argument);
}
@Parameters
public static Collection<Object[]> getData(){
System.out.println("Inside parameter");
String addThis;
if(initializeThis)
addThis="adding true";
else
addThis="adding false";
Object[] para1 = new Object[]{"First parameter :: " + addThis};
Object[] para2 = new Object[]{"Second parameter :: " + addThis};
Collection<Object[]> classNames = new ArrayList<Object[]>();
classNames.add(para1);
classNames.add(para2);
return classNames;
}
}
我知道的唯一缺点是,继承自该类的类将无法覆盖静态初始值设定项,而@BeforeClass在此方面具有一定的自由度;
Only drawback I know is that classes inherited from this won't be able to override static initializer, while @BeforeClass gives some freedom in this aspect;
这篇关于@parameters方法在@beforeclass方法之前执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!