I am writing JUnit Test case , where the process trying to get DataSource it is getting the following error message.
Stack Trace:
java.lang.NullPointerException
at play.api.db.DB$.getDataSource(DB.scala:141)
at play.api.db.DB.getDataSource(DB.scala)
at play.db.DB.getDataSource(DB.java:22)
at dao.BaseDao.getJdbcTemplate(BaseDao.java:13)
Here, I am using spring jdbctemplate, play framework and JUnit. please find the following resource files which I am using.
application.conf
db.default.jndiName=DefaultDS
db.default.driver=oracle.jdbc.driver.OracleDriver
db.default.url="jdbc:oracle:thin:@//xx.xx.xx.xx:1521/XE"
db.default.user=work
db.default.pass=work
......
components.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">
<context:component-scan
base-package="controllers,services,dao,org.springframework.jndi" />
</beans>
BaseDao.java
package dao;
import org.springframework.jdbc.core.JdbcTemplate;
import play.db.DB;
public class BaseDao {
private JdbcTemplate jdbcTemplate;
public JdbcTemplate getJdbcTemplate() {
if (jdbcTemplate == null) {
this.jdbcTemplate = new JdbcTemplate(DB.getDataSource("default"));
}
return this.jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
}
PublishedReferenceYieldServiceImplTest.java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:components.xml")
public class PublishedReferenceYieldServiceImplTest {
//Here I am accessing baseDAO
...
}
Seems you're missing to load the context of the application. Check the play.test.Helpers package.
Try putting your actual test code inside the following block:
running(fakeApplication(), new Runnable() {
@Override
public void run() {
...your test here...
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.