简体   繁体   中英

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. The problem is that this is for an framework (extension), and and an implementation of SomeInterface is to be provided by the user.

You set the value of the static member just in the constructor. So before not having at least one object of that class, you won't be able to access someValue() . In Junit the @Before annotation might be useful which is executed before each test and is not static .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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