读取Java属性文件后,值将继续为null

我试图从Java属性文件中读取值,以便可以在整个测试中全部使用它们。我有读取属性的BaseTest。在读取BaseTest中的值后立即打印时,这些值会正确显示。但是,当我尝试在扩展BaseTest的test1中访问它们时,该值为null。我有一个Java类,它接受像模板一样的值,没有初始化任何东西。这些值将填充TestValuesReader类。如何使值在整个代码中持久存在/不为null?

 public class test1 extends BaseTest {
        String var1 = TestValues.VAR1;

        @Test
        public void someFunction() throws InterruptedException {
            System.out.println(var1);
        }
}

public class BaseTest{

    public void readValues() throws IOException {
        Properties p = new Properties();
        InputStream is = new FileInputStream("TestValues.properties");
        p.load(is);
        TestValuesReader valuesReader = new TestValuesReader();
        valuesReader.readStrings(p);
    }

    @BeforeClass
    public void setUp() throws IOException {
        readValues();
        System.out.println(TestValues.VAR1); //this will give back the correct value, but when called
        //in someFunction() it is null
    }
}

public class TestValues{
    public static String VAR1; //not initialized, supposed to be ready from properties file
}

public class TestValuesReader {
    public void readStrings(Properties p) {
          TestValues.VAR1 = p.getProperty("VAR1");
    }
}




FAILED: checkForCandidate
java.lang.IllegalArgumentException: Keys to send should be a not null CharSequence
    at org.openqa.selenium.remote.RemoteWebElement.sendKeys(RemoteWebElement.java:97)
    at com.iai.test.pages.SpatialQuery.setAimPointInput(SpatialQuery.java:50)
    at com.iai.test.tests.cgm.CG_AX_001.checkForCandidate(CG_AX_001.java:21)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:134)
    at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:597)
    at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:173)
    at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
    at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:816)
    at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:146)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
    at java.util.ArrayList.forEach(ArrayList.java:1257)
    at org.testng.TestRunner.privateRun(TestRunner.java:766)
    at org.testng.TestRunner.run(TestRunner.java:587)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:384)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:378)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:337)
    at org.testng.SuiteRunner.run(SuiteRunner.java:286)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1187)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1109)
    at org.testng.TestNG.runSuites(TestNG.java:1039)
    at org.testng.TestNG.run(TestNG.java:1007)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
评论
  • 哼~
    哼~ 回复

    You need a Static Initialization Block or a Constructor to invoke super.readValues(). Do it as follows:

    使用静态初始化块:

    public class Test1 extends BaseTest {
        String var1;
    
        {
            try {
                super.readValues();
            } catch (IOException e) {
                e.printStackTrace();
            }
            var1 = TestValues.VAR1;
        }
    
        @Test
        public void someFunction() throws InterruptedException {
            System.out.println(var1);
        }
    }
    

    使用构造函数:

    public class Test1 extends BaseTest {
        String var1;
    
        Test1() {
            try {
                super.readValues();
                var1 = TestValues.VAR1;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        @Test
        public void someFunction() throws InterruptedException {
            System.out.println(var1);
        }
    }
    

    我已经测试了两种解决方案都可以正常工作。如有任何疑问/问题,请随时发表评论。

    补充说明:

    1. Make sure you have some value against the key, VAR1 e.g. VAR1=x in the properties file.
    2. I also suggest you follow Java naming conventions e.g. class test1 should be class Test1 as per the naming conventions.