我正在使用以下代码与数据库建立连接,但工作正常,但是当我使用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;
}
}