[英]Get past static field of jUnit:s @BeforeClass
有没有解决经典问题的方法
public class SomeClass implements SomeOtherInterface {
private static SomeInterface some;
public SomeClass(SomeInterface someInterface) {
some = someInterface;
}
@BeforeClass
public static void doSomethingWithInterface() {
System.out.println(someInterface.someValue()); // prints null
}
}
除了交换
System.out.println(someInterface.someValue()); // prints null
与
System.out.println(SomeInterface.someValue());
如果someValue是静态的。 问题在于这是针对框架(扩展)的,并且用户将提供SomeInterface的实现。
您只需在构造函数中设置static
成员的值。 因此,在没有该类的至少一个对象之前,您将无法访问someValue()
。 在Junit中, @Before
注释可能有用,它在每次测试之前执行,并且不是static
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.