简体   繁体   中英

reading a variable in method marked @Parameterized.Parameters

I have a method marked with annotation @Parameterized.Parameters

@Parameterized.Parameters
public static List<Object[]> myData() {

Now as part of another method marked with @BeforeClass, I read SystemProperty xyz

@BeforeClass
final String xyz = System.getProperty("XYZ");

If there anyway I can use xyz in myData so get List which depends on xyz? Something like making sure that Parameters are populated only after BeforeClass is done ?

If you have to set only "xyz" String - don't use @BeforeClass because it can be used only on static methods (your example is actually not correct).

So, define "xyz" as static final without @BeforeClass annotation:

private static final String xyz = System.getProperty("XYZ");

It would be accessible in myData() with value from that property.

Unfortunately, it's not possible to get the value inside myData() if you set "xyz" inside static method with @BeforeClass because that method would be invoked after myData() (in that case "xyz" would be null). As a workaround you can use static initialization block instead.

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