英文:
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);
}
}
我已经测试了这两种解决方案,它们按预期工作。
附加说明:
- 确保在属性文件中的键
VAR1
对应有某个值,例如在属性文件中设置VAR1=x
。 - 我建议您遵循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:
- Make sure you have some value against the key,
VAR1
e.g.VAR1=x
in the properties file. - I also suggest you follow Java naming conventions e.g.
class test1
should beclass Test1
as per the naming conventions.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论