Frühjahr PropertySource nicht in der JUnit-Test

Ich bin vor Problem während der Ausführung von JUnit-Testfall.

Problem ist,
Wenn ich die Anwendung als web-Anwendung dann @PropertySource funktioniert durch die Injektion alle Eigenschaften von xml-Datei aber wenn ich die Anwendung ausführen als JUnit-dann bin ich immer der Fehler "Konnte nicht aufgelöst werden Platzhalter".

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-Klasse

@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-Datei

<?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-Test-Klasse

@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();
        ............  
    }
}

spring-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

Wenn ich den JUnit-Test-Datei, erhalte ich die exception so,

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

zwar funktioniert alles wunderbar, wenn ich das gleiche Projekt als Web-Anwendung.

  • Ich sehe nicht, wie dein test ist in Bezug auf die anderen Dateien, die du gepostet hast. Warum hast du post web.xml? Was sind die zwei applicationContext.xml und spring-mvc-context.xml?
  • fügte hinzu, dass 2 Dateien. Ich bin nicht immer, warum JUnit ist nicht in der Lage, autowire Felder als ich bin laden, die Klasse
InformationsquelleAutor Jayesh | 2015-03-15
Schreibe einen Kommentar