簡體   English   中英

JUnit測試用例問題與獲取數據庫連接以及播放框架一起使用,春天jdbctemplate?

[英]JUnit test case issue with fetching db connection along with play framework, spring jdbctemplate?

我正在編寫JUnit測試用例 ,其中嘗試獲取DataSource的過程正在獲取以下錯誤消息。

堆棧跟蹤:

   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)

在這里,我正在使用spring jdbctemplate,play框架和JUnit。 請找到我正在使用的以下資源文件。

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
...
}

似乎您缺少加載應用程序的上下文。 檢查play.test.Helpers包。

嘗試將您的實際測試代碼放入以下塊中:

running(fakeApplication(), new Runnable() {
    @Override
    public void run() {
        ...your test here...
    }           
}); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM