开发者社区> 问答> 正文

使用junit测试数据源时出现空指针异常:报错

在spring项目中使用druid作为连接池,配置数据源,

如题所示,在spring中配置druid作为数据源。以下是我的xml配置文件

<!--引入jdbc配置文件--> <context:property-placeholder location="classpath:databaseConfig.properties" ignore-unresolvable="true"/> 	 <!-- 使用Druid连接池 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- 配置初始化大小、最小、最大 --> <property name="initialSize" value="${initialSize}" /> <property name="minIdle" value="${minIdle}" /> <property name="maxActive" value="${maxActive}" /> <!-- 配置获取连接等待超时的时间 --> <property name="maxWait" value="${maxWait}" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}" /> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis}" /> <property name="validationQuery" value="${validationQuery}" /> <property name="testWhileIdle" value="${testWhileIdle}" /> <property name="testOnBorrow" value="${testOnBorrow}" /> <property name="testOnReturn" value="${testOnReturn}" /> <!-- 打开PSCache,并且指定每个连接上PSCache的大小 --> <property name="poolPreparedStatements" value="${poolPreparedStatements}" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="${maxPoolPreparedStatementPerConnectionSize}" /> <!-- 配置监控统计拦截的filters --> <property name="filters" value="${filters}" /> </bean>

以下是我的databaseConfig.properties属性文件,为方便提问,账号密码等有做修改。

jdbc.driverClassName = oracle.jdbc.driver.OracleDriver
jdbc.url = jdbc:oracle:thin:@10.153.123.169:1521:SZQX
jdbc.username = username
jdbc.password = password
<!--连接池初始化数量-->
initialSize = 10
<!--最大并发数量-->
maxActive = 10
<!-最小空闲连接数->  
minIdle = 4
<!-- 配置获取连接等待超时的时间 --> 
maxWait = 60000
 <!-- 超过时间限制是否回收 -->
removeAbandoned = true
<!-- 超过时间限制多长; -->
removeAbandonedTimeout = 180
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
timeBetweenEvictionRunsMillis = 60000
 <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
minEvictableIdleTimeMillis = 300000
 <!-- 用来检测连接是否有效的sql,要求是一个查询语句-->
validationQuery = SELECT 1 FROM DUAL
 <!-- 申请连接的时候检测 -->
testWhileIdle = true 
<!-- 申请连接时执行validationQuery检测连接是否有效,配置为true会降低性能 -->
testOnBorrow = false
<!-- 归还连接时执行validationQuery检测连接是否有效,配置为true会降低性能  -->
testOnReturn = false
<!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
poolPreparedStatements = true
maxPoolPreparedStatementPerConnectionSize = 50
<!-- 配置监控统计拦截的filters -->
filters = log4j,stat

junit 测试代码:

public class Test {
	@org.junit.Test
	public void test(){
        BeanFactory factory = new ClassPathXmlApplicationContext("dataSource.xml");   
        DataSource ds = (DataSource) factory.getBean("dataSource");  
        
        try {
			Connection conn = ds.getConnection();
			PreparedStatement ps = conn.prepareStatement("select sysdate from dual");
			ResultSet rs = ps.executeQuery();
			while(rs.next()){
				System.out.println(rs.getString(1));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}



junit测试结果:
Test (3)
Test
test(Test)
java.lang.NullPointerException

	at com.alibaba.druid.filter.logging.LogFilter.connection_connectAfter(LogFilter.java:349)

	at com.alibaba.druid.filter.FilterEventAdapter.connection_connect(FilterEventAdapter.java:40)

	at com.alibaba.druid.filter.FilterChainImpl.connection_connect(FilterChainImpl.java:142)

	at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1377)

	at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1431)

	at com.alibaba.druid.pool.DruidDataSource.init(DruidDataSource.java:632)

	at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:934)

	at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:930)

	at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:102)

	at Test.test(Test.java:25)

	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

	at java.lang.reflect.Method.invoke(Method.java:606)

	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)

	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)

	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)

	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)

	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)

	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)

	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)

	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)

	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)

	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)

	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)

	at org.junit.runners.ParentRunner.run(ParentRunner.java:309)

	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)

	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)

	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)

	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)

	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)

	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

junit 测试时,获取到的dataSource 对象属性。

pom文件:


    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>compile</scope>
    </dependency>    
    
    <dependency>
		<groupId>com.alibaba</groupId>
		<artifactId>fastjson</artifactId>
		<version>1.2.4</version>
   </dependency>
    
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-context</artifactId>
		<version>4.1.1.RELEASE</version>
		<scope>compile</scope>	
	</dependency>
	
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-web</artifactId>
		<version>4.0.3.RELEASE</version>
		<scope>compile</scope>		
	</dependency>

	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-webmvc</artifactId>
		<version>4.0.3.RELEASE</version>
	    <scope>compile</scope>	
	</dependency>

	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-context-support</artifactId>
		<version>4.1.1.RELEASE</version>
	</dependency>
	
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-aop</artifactId>
		<version>4.2.1.RELEASE</version>
	</dependency>
	
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-jdbc</artifactId>
		<version>4.2.1.RELEASE</version>
	</dependency>
	
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-tx</artifactId>
		<version>4.2.1.RELEASE</version>
	</dependency>	

	<dependency>
		<groupId>org.jboss.spec.javax.annotation</groupId>
		<artifactId>jboss-annotations-api_1.1_spec</artifactId>
		<version>1.0.0.Final</version>
		<scope>compile</scope>
	</dependency>
	
	<dependency>
		<groupId>org.codehaus.jackson</groupId>
		<artifactId>jackson-mapper-asl</artifactId>
		<version>1.9.13</version>
	</dependency>
	
	<dependency>
	<groupId>org.aspectj</groupId>
	<artifactId>aspectjweaver</artifactId>
	<version>1.8.7</version>
</dependency>

	<dependency>
		<groupId>dom4j</groupId>
		<artifactId>dom4j</artifactId>
		<version>1.6.1</version>
		<scope>compile</scope>
	</dependency>
        
	<dependency>
		<groupId>log4j</groupId>
		<artifactId>log4j</artifactId>
		<version>1.2.17</version>
	</dependency>
	
	<dependency>
		<groupId>commons-codec</groupId>
		<artifactId>commons-codec</artifactId>
		<version>1.10</version>
	</dependency>
	
        
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.2.8</version>
      <scope>compile</scope>
    </dependency>
    
    <dependency>
		<groupId>org.mybatis</groupId>
		<artifactId>mybatis-spring</artifactId>
		<version>1.2.3</version>
	</dependency>
    
	<dependency>
		<groupId>com.alibaba</groupId>
		<artifactId>druid</artifactId>
		<version>1.0.15</version>
	</dependency>    
        
    <dependency>
      <groupId>com.oracle</groupId>
      <artifactId>ojdbc6</artifactId>
      <version>11.2.0</version>
      <scope>compile</scope>
    </dependency>
        
	<dependency>
		<groupId>org.quartz-scheduler</groupId>
		<artifactId>quartz</artifactId>
		<version>2.2.1</version>
	</dependency>    
	
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-tx</artifactId>
		<version>4.1.1.RELEASE</version>
	</dependency>

	<dependency>
		<groupId>org.mybatis</groupId>
		<artifactId>mybatis</artifactId>
		<version>3.3.0</version>
	</dependency>



求指导!


展开
收起
kun坤 2020-06-08 11:11:04 623 0
1 条回答
写回答
取消 提交回答
  • 已经解决,一个地方写错,文中错误已经修改过来,感谢各位帮忙查错. 文中配置已经可以运行,可供参考######

    引用来自“暴怒的栗子”的评论

    已经解决,一个地方写错,文中错误已经修改过来,感谢各位帮忙查错. 文中配置已经可以运行,可供参考
    就是这样!
    2020-06-08 14:21:33
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
移动互联网测试到质量的转变 立即下载
给ITer的技术实战进阶课-阿里CIO学院独家教材(四) 立即下载
F2etest — 多浏览器兼容性测试整体解决方案 立即下载