【问题标题】:Why is autowired properties file null?为什么自动装配的属性文件为空?
【发布时间】:2015-05-04 13:38:25
【问题描述】:

我从我的 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:

I made a mistake everithing is null when the selector is null.I cheked Initialization in my test runs before autowired, somehow this

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 自动装配后运行。有什么建议吗?

【问题讨论】:

    标签: java xml spring unit-testing autowired


    【解决方案1】:

    尝试将标签&lt;context:annotation-config/&gt; 下移一行。我不确定,但似乎此标记触发的连接发生在 创建seleniumSelectors 之前,所以目前它仍然是null

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

    【讨论】:

    • 我试图在 下移动 但仍然给出 null
    • 尝试用类java.util.Properties的常规bean定义临时替换&lt;util:properties
    • 你能写一个例子吗?我不确定,因为我写得正确。我做了这样的事情但没有工作: 并在类中 @Autowired protected Properties seleniumSelectors;
    猜你喜欢
    • 2013-09-28
    • 2018-04-16
    • 2015-12-02
    • 2016-05-01
    • 1970-01-01
    • 2012-04-23
    • 2016-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多