dimanche 15 mars 2015

Spring PropertySource not working in JUnit Test

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://ift.tt/UuNDt1">
<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();
............
}
}


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.


Aucun commentaire:

Enregistrer un commentaire