读取Java属性文件时,值始终变为null。

huangapple go评论90阅读模式
英文:

Reading a java properties file, values keep going to 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)
英文:

I am trying to read in values from a java properties file so I can use them all throughout my tests. I have BaseTest which reads in the properties. When I print right after I read the values in BaseTest, the values show correctly. But when I try to access them in test1 which extends BaseTest, the values are null. I have a java class that accepts the values like a template, nothing initialized. The values are populated with the TestValuesReader class. How can I have the values persist all throughout the code/not be 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)

答案1

得分: 2

使用静态初始化块或构造函数来调用 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命名约定,例如 class test1 应该改为 class Test1,以符合命名约定。
英文:

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

Using a Static Initialization Block:

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

Using a Constructor:

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

I've tested both the solutions to be working as expected.

Additional notes:

  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.

huangapple
  • 本文由 发表于 2020年4月9日 04:51:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/61109815.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定