簡體   English   中英

Spring PropertySource在JUnit測試中不起作用

[英]Spring PropertySource not working in JUnit Test

我在運行JUnit測試用例時遇到問題。

問題是,當我運行應用程序作為Web應用程序,然后@PropertySource的工作原理是注射從XML文件中的所有性能優良,但是當我運行應用程序的JUnit然后我收到錯誤為“無法解析占位符”。

web.xml中

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:resources/applicationContext.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:resources/spring-mvc-context.xml</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

DAO類

@Configuration
@PropertySource("classpath:resources/shore-dal_queries.xml")
public class SpringShoreDao extends SpringBaseDao implements ShoreDao {

    @Value("${shore.getAllShores}")
    private String getShoresQuery;

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    @Override
    public Optional<List<Shore>> getAllShores() throws DataAccessException {
        Optional<List<Shore>> shoreList = null;
        try {
            List<Shore> tempList = (getNamedParameterJdbcTemplate().query(getShoresQuery, new ShoreRowMapper()));
            shoreList = Optional.of(tempList);
        }
        catch (org.springframework.dao.DataAccessException e) {
        }
        return shoreList;
    }
}

XML文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
    <entry key="shore.getAllShores">
        <![CDATA[
            SELECT shoreID, shoreName, isActive, updatedBy, updatedDate, createdBy, createdDate
            FROM shore
        ]]>
    </entry>
</properties>

JUnit測試類

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
        "classpath:resources/applicationContext.xml",
        "classpath:resources/spring-mvc-context.xml"})
public class SpringShoreDaoTest {


    @Autowired
    ShoreDao shoreDao; 

    @Test
    public void testGetAllShores() throws DataAccessException {
        List<Shore> listShore= shoreDao.getAllShores();
        ............  
    }
}

彈簧MVC-context.xml中

  <annotation-driven />
    <context:annotation-config />
    <context:component-scan base-package="com.abc.him.utmc" />     
    <resources mapping="/resources/**" location="/resources/" />

    <beans:bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <beans:property name="messageConverters">
            <beans:list>
                <beans:ref bean="jsonMessageConverter"/>
            </beans:list>
        </beans:property>
    </beans:bean>

    <beans:bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
    </beans:bean> 

applicationContext.xml中

<context:annotation-config />
<context:property-placeholder location="classpath:resources/db.properties"/>
... DB related beans

運行JUnit測試文件時,出現異常,

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'shore.getAllShores' in string value "${shore.getAllShores}"

當我運行與Web應用程序相同的項目時,一切正常。

您可以定義PropertySourcesPlaceholderConfigurer測試配置和設置您的resources/shore-dal_queries.xml作為資源給它(有很好的例子在這里 )。

所以嘗試這樣的事情:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class SpringShoreDaoTest {

    @Configuration
    @ImportResource({"classpath:resources/applicationContext.xml",
    "classpath:resources/spring-mvc-context.xml"})   
    static class someConfig {

        // because @PropertySource doesnt work in annotation only land
        @Bean
        public static PropertyPlaceholderConfigurer propConfig() {
            PropertyPlaceholderConfigurer ppc =  new PropertyPlaceholderConfigurer();
            ppc.setLocation(new ClassPathResource("resources/shore-dal_queries.xml"));
            return ppc;
        }

        @Bean
        public SpringShoreDao springShoreDao() {
            //probably you do not need this bean since you have component scan in applicationContext 
            return new SpringShoreDao();
        }
    }


    @Autowired
    SpringShoreDao springShoreDao; 

    @Test
    public void testGetAllShores() throws DataAccessException {
        List<Shore> listShore= shoreDao.getAllShores();
        ............  
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM