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

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

Reading a java properties file, values keep going to null

问题

  1. public class test1 extends BaseTest {
  2. String var1 = TestValues.VAR1;
  3. @Test
  4. public void someFunction() throws InterruptedException {
  5. System.out.println(var1);
  6. }
  7. }
  8. public class BaseTest {
  9. public void readValues() throws IOException {
  10. Properties p = new Properties();
  11. InputStream is = new FileInputStream("TestValues.properties");
  12. p.load(is);
  13. TestValuesReader valuesReader = new TestValuesReader();
  14. valuesReader.readStrings(p);
  15. }
  16. @BeforeClass
  17. public void setUp() throws IOException {
  18. readValues();
  19. System.out.println(TestValues.VAR1); //this will give back the correct value, but when called
  20. //in someFunction() it is null
  21. }
  22. }
  23. public class TestValues {
  24. public static String VAR1; //not initialized, supposed to be ready from properties file
  25. }
  26. public class TestValuesReader {
  27. public void readStrings(Properties p) {
  28. TestValues.VAR1 = p.getProperty("VAR1");
  29. }
  30. }
  31. FAILED: checkForCandidate
  32. java.lang.IllegalArgumentException: Keys to send should be a not null CharSequence
  33. at org.openqa.selenium.remote.RemoteWebElement.sendKeys(RemoteWebElement.java:97)
  34. at com.iai.test.pages.SpatialQuery.setAimPointInput(SpatialQuery.java:50)
  35. at com.iai.test.tests.cgm.CG_AX_001.checkForCandidate(CG_AX_001.java:21)
  36. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  37. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  38. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  39. at java.lang.reflect.Method.invoke(Method.java:498)
  40. at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:134)
  41. at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:597)
  42. at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:173)
  43. at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
  44. at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:816)
  45. at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:146)
  46. at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
  47. at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
  48. at java.util.ArrayList.forEach(ArrayList.java:1257)
  49. at org.testng.TestRunner.privateRun(TestRunner.java:766)
  50. at org.testng.TestRunner.run(TestRunner.java:587)
  51. at org.testng.SuiteRunner.runTest(SuiteRunner.java:384)
  52. at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:378)
  53. at org.testng.SuiteRunner.privateRun(SuiteRunner.java:337)
  54. at org.testng.SuiteRunner.run(SuiteRunner.java:286)
  55. at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
  56. at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
  57. at org.testng.TestNG.runSuitesSequentially(TestNG.java:1187)
  58. at org.testng.TestNG.runSuitesLocally(TestNG.java:1109)
  59. at org.testng.TestNG.runSuites(TestNG.java:1039)
  60. at org.testng.TestNG.run(TestNG.java:1007)
  61. at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
  62. at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
  63. 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?

  1. public class test1 extends BaseTest {
  2. String var1 = TestValues.VAR1;
  3. @Test
  4. public void someFunction() throws InterruptedException {
  5. System.out.println(var1);
  6. }
  7. }
  8. public class BaseTest{
  9. public void readValues() throws IOException {
  10. Properties p = new Properties();
  11. InputStream is = new FileInputStream("TestValues.properties");
  12. p.load(is);
  13. TestValuesReader valuesReader = new TestValuesReader();
  14. valuesReader.readStrings(p);
  15. }
  16. @BeforeClass
  17. public void setUp() throws IOException {
  18. readValues();
  19. System.out.println(TestValues.VAR1); //this will give back the correct value, but when called
  20. //in someFunction() it is null
  21. }
  22. }
  23. public class TestValues{
  24. public static String VAR1; //not initialized, supposed to be ready from properties file
  25. }
  26. public class TestValuesReader {
  27. public void readStrings(Properties p) {
  28. TestValues.VAR1 = p.getProperty("VAR1");
  29. }
  30. }
  31. FAILED: checkForCandidate
  32. java.lang.IllegalArgumentException: Keys to send should be a not null CharSequence
  33. at org.openqa.selenium.remote.RemoteWebElement.sendKeys(RemoteWebElement.java:97)
  34. at com.iai.test.pages.SpatialQuery.setAimPointInput(SpatialQuery.java:50)
  35. at com.iai.test.tests.cgm.CG_AX_001.checkForCandidate(CG_AX_001.java:21)
  36. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  37. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  38. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  39. at java.lang.reflect.Method.invoke(Method.java:498)
  40. at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:134)
  41. at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:597)
  42. at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:173)
  43. at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
  44. at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:816)
  45. at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:146)
  46. at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
  47. at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
  48. at java.util.ArrayList.forEach(ArrayList.java:1257)
  49. at org.testng.TestRunner.privateRun(TestRunner.java:766)
  50. at org.testng.TestRunner.run(TestRunner.java:587)
  51. at org.testng.SuiteRunner.runTest(SuiteRunner.java:384)
  52. at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:378)
  53. at org.testng.SuiteRunner.privateRun(SuiteRunner.java:337)
  54. at org.testng.SuiteRunner.run(SuiteRunner.java:286)
  55. at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
  56. at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
  57. at org.testng.TestNG.runSuitesSequentially(TestNG.java:1187)
  58. at org.testng.TestNG.runSuitesLocally(TestNG.java:1109)
  59. at org.testng.TestNG.runSuites(TestNG.java:1039)
  60. at org.testng.TestNG.run(TestNG.java:1007)
  61. at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
  62. at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
  63. at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

答案1

得分: 2

使用静态初始化块或构造函数来调用 super.readValues()。可以按照以下方式操作:

使用静态初始化块:

  1. public class Test1 extends BaseTest {
  2. String var1;
  3. {
  4. try {
  5. super.readValues();
  6. } catch (IOException e) {
  7. e.printStackTrace();
  8. }
  9. var1 = TestValues.VAR1;
  10. }
  11. @Test
  12. public void someFunction() throws InterruptedException {
  13. System.out.println(var1);
  14. }
  15. }

使用构造函数:

  1. public class Test1 extends BaseTest {
  2. String var1;
  3. Test1() {
  4. try {
  5. super.readValues();
  6. var1 = TestValues.VAR1;
  7. } catch (IOException e) {
  8. e.printStackTrace();
  9. }
  10. }
  11. @Test
  12. public void someFunction() throws InterruptedException {
  13. System.out.println(var1);
  14. }
  15. }

我已经测试了这两种解决方案,它们按预期工作。

附加说明:

  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:

  1. public class Test1 extends BaseTest {
  2. String var1;
  3. {
  4. try {
  5. super.readValues();
  6. } catch (IOException e) {
  7. e.printStackTrace();
  8. }
  9. var1 = TestValues.VAR1;
  10. }
  11. @Test
  12. public void someFunction() throws InterruptedException {
  13. System.out.println(var1);
  14. }
  15. }

Using a Constructor:

  1. public class Test1 extends BaseTest {
  2. String var1;
  3. Test1() {
  4. try {
  5. super.readValues();
  6. var1 = TestValues.VAR1;
  7. } catch (IOException e) {
  8. e.printStackTrace();
  9. }
  10. }
  11. @Test
  12. public void someFunction() throws InterruptedException {
  13. System.out.println(var1);
  14. }
  15. }

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:

确定