Reputation: 6111
I am facing issue while running JUnit test case.
Problem is,
When I run application as web application then @PropertySource
works fine by injecting all properties from xml file but when I run the application as JUnit then I am getting error as "Could not resolve placeholder".
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 Class
@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 File
<?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 Class
@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
When I run the JUnit Test File, I am getting exception as,
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'shore.getAllShores' in string value "${shore.getAllShores}"
while everything works fine when I run the same project as Web Application.
Upvotes: 1
Views: 7140
Reputation: 3784
You can define PropertySourcesPlaceholderConfigurer
in test configuration and set your resources/shore-dal_queries.xml
as resource to it (there is nice example here).
So try something like this:
@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();
............
}
}
Upvotes: 1