为什么autowired属性文件为null?

时间:2015-05-04 13:38:25

标签: java xml spring unit-testing autowired

我从testConfig.xml中自动安装了一些bean,它工作正常但是当我想自动装配属性文件时它给出null,属性文件在selenium文件夹中接近我的xml。我的testConfig.xml看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util" 
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd" >

    <bean id="base" class="java.net.URI">
        <constructor-arg value="http://localhost:8080/MySite" />
    </bean>

    <bean id="siteBase" class="java.net.URI">
        <constructor-arg value="http://localhost:8080/MySite/site" />
    </bean>

    <bean id="adminBase" class="java.net.URI">
        <constructor-arg value="http://localhost:8080/MySite/admin" />
    </bean>

    <bean id="firefoxDriver" class="org.openqa.selenium.firefox.FirefoxDriver" destroy-method="quit"/>

    <context:annotation-config/>
    <util:properties id="seleniumSelectors" location="classpath:selenium/selenium-selectors.properties"/>
</beans>

在这里我想要自动装配它:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/testConfig.xml" })
public abstract class AbstractUITest extends TestCase{


    @Autowired
    protected URI base;


    @Autowired
    protected URI siteBase;


    @Autowired
    protected URI adminBase;


    @Autowired
    protected WebDriver firefoxDriver;

    @Autowired
    @Qualifier("seleniumSelectors")
    protected Properties selectors;

    protected By getBySelectorKey(String key){
        if(key.endsWith(".xpath")){
            return By.xpath(selenium.getProperty(key));
        }
    }

只是选择器对象为空,我不知道为什么,任何建议?

更新2:

我犯了一个错误,当选择器为null时,everithing为null。我在自动装配之前的测试运行中进行了初始化,不知怎的,这个

public class AdminCandidatesPageUITest extends AbstractAdminUITest {

    private By COMPONENT_QUERY_TEXTBOX_EMAIL = getBySelectorKey("admin.candidates.edit.textbox.email.xpath");
    private By COMPONENT_QUERY_TEXTBOX_EMAIL_ERROR = getBySelectorKey("admin.candidates.edit.textbox.email.errors.xpath");
应在seleniumSelector自动装配后运行。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

尝试将标记<context:annotation-config/>向下移动一行。我不确定但是这个标签触发的布线似乎在创建seleniumSelectors之前发生,所以目前它仍然是null

BTW如果您将属性标记为@Required,则上下文将无法启动并抛出异常,因为其中一个属性未连线。这可能比稍后在运行时失败更可取。