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
undspring-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
Du musst angemeldet sein, um einen Kommentar abzugeben.
Können Sie definieren
PropertySourcesPlaceholderConfigurer
im test-Konfiguration und stellen Sie Ihreresources/shore-dal_queries.xml
als Ressource zu (es ist schönes Beispiel hier).Also versuchen Sie so etwas wie dieses:
applicationContext.xml
undspring-mvc-context.xml
weil ich nicht sehen, wie Sie verbunden sind mit der dao-Klasse allein und warum Sie benötigt werden. Wenn Sie müssen Sie als definierenPropertyPlaceholderConfigurer
im test nur xml und fügen Sie ihn auf zu@ContextConfiguration
nach zwei xml-Ressourcen.@ImportResource
wie hier vorgeschlagen stackoverflow.com/questions/19365366/...@Configuration
das ist seltsam... Und Sie neu definieren PropertySourcesPlaceholderConfigurer bean jedes mal. Sollten Sie über configuration-Klasse, die definiert PropertySourcesPlaceholderConfigurer, Eigenschaften usw. und definiert SpringShoreDao als Komponente, die verwendet nur@Value
injiziert, wäre das bessere design, und Sie würden im test problemlos.