【问题标题】:Reading a java properties file, values keep going to null读取 java 属性文件,值一直为空
【发布时间】:2020-04-08 20:51:30
【问题描述】:

我正在尝试从 java 属性文件中读取值,以便在整个测试过程中使用它们。我有读取属性的 BaseTest。当我在读取 BaseTest 中的值后立即打印时,这些值显示正确。但是当我尝试在扩展 BaseTest 的 test1 中访问它们时,这些值为空。我有一个 java 类,它接受像模板一样的值,没有初始化。这些值由 TestValuesReader 类填充。如何让值在整个代码中保持不变/不为空?

 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)

【问题讨论】:

  • 您应该在使用InputStream.close();try-with-resource 阅读后关闭流。但这并不能回答你的问题。你有 Stacktrace 吗?
  • @LinuxServer 我添加了一个堆栈跟踪,但它告诉我我已经知道我的测试正在尝试将键盘输入发送到某个 Web 对象但值为空。我认为该错误与我们退出 BaseTest 中发生的任何事情并继续进行 test1 后值没有持续存在的事实有关。
  • 如果没有堆栈跟踪,我无法帮助您。
  • @LinuxServer 堆栈跟踪已添加到代码下

标签: java oop null testng properties-file


【解决方案1】:

您需要一个Static Initialization Block or a Constructor 来调用super.readValues()。按如下方式进行:

使用静态初始化块:

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. 确保您对密钥有一定的价值,VAR1 例如VAR1=x 在属性文件中。
  2. 我也建议你关注Java naming conventions,例如根据命名约定,class test1 应为 class Test1

【讨论】:

  • 成功了,谢谢!有什么办法可以按照我尝试的方式进行操作,并将所有值分别分配给 TestValues 类,然后在分配到不同的类后调用它们?
  • 不客气。这是推荐的方法。根据您设计其他类的方式,可以有不同的方法来调用readValues(),但这是您要调用的关键函数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-16
  • 2012-01-07
  • 1970-01-01
  • 2011-09-08
  • 1970-01-01
  • 2017-07-20
  • 2013-10-25
相关资源
最近更新 更多