Java 无法使用Spring属性保持器读取属性

Java 无法使用Spring属性保持器读取属性,java,spring,properties,selenium-webdriver,Java,Spring,Properties,Selenium Webdriver,我试图在maven java项目中使用属性文件实现测试自动化 这是context.xml文件 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://

我试图在maven java项目中使用属性文件实现测试自动化

这是context.xml文件

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:util="http://www.springframework.org/schema/util"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/util
       http://www.springframework.org/schema/util/spring-util-2.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/context ">

<util:properties id="properties" location="classpath:test-context.properties"/>

<context:property-placeholder properties-ref="properties" ignore-unresolvable="false"/>


<bean id="settings" class="util.TestSettings">
    <property name="properties" ref="properties"/>
</bean>


更新 这是我的context.property文件

base.url=http://uname:pword@test.mysite.com.au/sdfdf
email.Queue.url=http://uname:pword@test.mysite.com.au/admin/admin/show_msgs
当我尝试运行测试用例时,这里会出现一个空指针异常

public static String getProperty(String key) {
    return properties.getProperty(key);
}

有人能帮我解决这个问题吗?

您的代码打破了Spring的一些指导原则,例如用于访问属性的静态字段,或者在容器外部使用新操作符创建
p\u 1\u登录页面。但NullPointer的主要问题是,Spring上下文尚未在
@BeforeClass
中初始化(方法也应该是静态的,否则它将导致异常)。将
@BeforeClass
替换为
@BeforeClass

该函数中的空值是多少?“属性”或者它只是没有通过“键”找到值?@THarms:我是编程新手,我不能准确地找到它。但是它显示在“return properties.getProperty(key);”行中。我已映射属性文件,请参见更新的问题。我认为映射值和键是正确的。感谢在测试中使用属性的完整方式(使用spring)是错误的。-很抱歉,由于时间不够,我没有时间详细描述,但我希望这些信息能帮助您朝着正确的方向搜索。米卡拉非常感谢。正如您提到的,问题是“Spring上下文尚未在BeforeClass中初始化”。因为它是一个testng注释,所以我没有使用“Before”。我所做的是,在测试用例类中初始化Spring上下文,然后修复它。
base.url=http://uname:pword@test.mysite.com.au/sdfdf
email.Queue.url=http://uname:pword@test.mysite.com.au/admin/admin/show_msgs
public static String getProperty(String key) {
    return properties.getProperty(key);
}