使用JUNIT

时间:2019-06-17 13:56:12

标签: java spring junit classloader jdbctemplate

我正在使用以下代码与数据库建立连接,但工作正常,但是当我使用junit测试此连接时,则在我使用junit测试此文件时会抛出空指针异常。

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.log4j.Logger; 
public class OpenDBConnectionPool {


  private static BasicDataSource createDataSource() { 
  BasicDataSource ds = new BasicDataSource();
    Properties prop1 = new Properties();
    InputStream input1 = null;
    String filename = "dbfile.properties";
    input1 = OpenDBConnectionPool.class.getClassLoader().getResourceAsStream(filename);
// here throw null pointer exception in getClassLoader while junit testing
    try {
        prop1.load(input1);
    } catch (IOException ex) {

       logger.error("Exception " + CommonUtil.stackTraceElementToString(ex));
    }
    String url = prop1.getProperty("URL");
    String username = prop1.getProperty("Username");
    String password = prop1.getProperty("Password");
    ds.setDriverClassName("oracle.jdbc.OracleDriver");
    ds.setUrl(url);
    ds.setUsername(username);
    ds.setPassword(password);


    return ds;

  }
 }

0 个答案:

没有答案