简体   繁体   English

获取jUnit:s @BeforeClass的静态字段

[英]Get past static field of jUnit:s @BeforeClass

Is there any way around the classic problem of 有没有解决经典问题的方法

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

other than exchanging 除了交换

System.out.println(someInterface.someValue()); // prints null

with

System.out.println(SomeInterface.someValue());

if someValue is static. 如果someValue是静态的。 The problem is that this is for an framework (extension), and and an implementation of SomeInterface is to be provided by the user. 问题在于这是针对框架(扩展)的,并且用户将提供SomeInterface的实现。

You set the value of the static member just in the constructor. 您只需在构造函数中设置static成员的值。 So before not having at least one object of that class, you won't be able to access someValue() . 因此,在没有该类的至少一个对象之前,您将无法访问someValue() In Junit the @Before annotation might be useful which is executed before each test and is not static . 在Junit中, @Before注释可能有用,它在每次测试之前执行,并且不是static

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM