很诡异的是空指针随机出现。两个单元测试类,可能两个都会在调用service时报空指针错误;也可能之只有一个报空指针错误;还有一种最奇怪的现象,只有一个方法报空指针,其他都测试通过;比如 这个 testSave;再贴一段运行log,帮忙一起分析下: cd D:\MavenProjects\babasportrefactoring\babasportbluetripe; JAVA_HOME=D:\\program\\jdk1.6 D:\\program\\apache-maven-3.0.3\\bin\\mvn.bat -Dnetbeans.deploy=true -Dnetbeans.deploy.clientUrlPart=/ package Scanning for projects...
------------------------------------------------------------------------ Building babasportbluetripe 1.0-SNAPSHOT ------------------------------------------------------------------------[resources:resources] Using 'UTF-8' encoding to copy filtered resources. Copying 24 resources[compiler:compile] Compiling 27 source files to D:\MavenProjects\babasportrefactoring\babasportbluetripe\target\classes[resources:testResources] Using 'UTF-8' encoding to copy filtered resources. Copying 10 resources[compiler:testCompile] Compiling 2 source files to D:\MavenProjects\babasportrefactoring\babasportbluetripe\target\test-classes[surefire:test] Surefire report directory: D:\MavenProjects\babasportrefactoring\babasportbluetripe\target\surefire-reports------------------------------------------------------- T E S T S ------------------------------------------------------- Concurrency config is parallel='methods', perCoreThreadCount=true, threadCount=10, useUnlimitedThreads=false 2011-10-8 20:37:53 org.springframework.test.context.TestContextManager retrieveTestExecutionListeners 锟愶繀锟忥劲: @TestExecutionListeners is not present for class [class com.itcast.service.product.BrandServiceTest]: using defaults. 2011-10-8 20:37:53 org.springframework.test.context.TestContextManager retrieveTestExecutionListeners 锟愶繀锟忥劲: @TestExecutionListeners is not present for class [class com.itcast.service.product.TypeServiceTest]: using defaults. Running com.itcast.service.product.TypeServiceTest 测试Find()方法! 2011-10-8 20:37:56 com.itcast.service.product.TypeServiceTest testFind 信息: 获取id为3的记录 com.itcast.model.product.Type@650646 测试Delete()方法! 测试Update()方法! 2011-10-8 20:37:56 com.itcast.service.product.TypeServiceTest testUpdate 信息: 更新 id为3的记录 com.itcast.model.product.Type@1b59919 当前产品类别名称为:6篮球产品;其id为 7 当前产品类别名称为:7篮球产品;其id为 8 当前产品类别名称为:8篮球产品;其id为 9 当前产品类别名称为:9篮球产品;其id为 10 当前产品类别名称为:10篮球产品;其id为 11 当前产品类别名称为:11篮球产品;其id为 12 当前产品类别名称为:12篮球产品;其id为 13 当前产品类别名称为:13篮球产品;其id为 14 当前产品类别名称为:14篮球产品;其id为 15 当前产品类别名称为:15篮球产品;其id为 16 测试save()方法! Tests run: 5, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.22 sec Running com.itcast.service.product.BrandServiceTest 2011-10-8 20:37:54 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [beans.xml] 2011-10-8 20:37:54 org.springframework.context.support.AbstractApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.GenericApplicationContext@30e280: startup date [Sat Oct 08 20:37:54 CST 2011]; root of context hierarchy 2011-10-8 20:37:54 org.springframework.core.io.support.PropertiesLoaderSupport loadProperties 信息: Loading properties file from class path resource [jdbc.properties] 2011-10-8 20:37:54 org.springframework.context.support.AbstractApplicationContext$BeanPostProcessorChecker postProcessAfterInitialization 信息: Bean 'dataSource' of type [class org.apache.commons.dbcp.BasicDataSource] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2011-10-8 20:37:54 org.springframework.context.support.AbstractApplicationContext$BeanPostProcessorChecker postProcessAfterInitialization 信息: Bean 'org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver#39443f' of type [class org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2011-10-8 20:37:54 org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean createNativeEntityManagerFactory 信息: Building JPA container EntityManagerFactory for persistence unit 'itcast' 2011-10-8 20:37:55 org.springframework.context.support.AbstractApplicationContext$BeanPostProcessorChecker postProcessAfterInitialization 信息: Bean 'entityManagerFactory' of type [class org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying) 2011-10-8 20:37:55 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@9cbd4b: defining beans [brandService,typeService,uploadFileService,/control/product/allbrandsfinder,/control/product/alltypesfinder,/control/product/branddeleter,/control/product/brandfinder,/control/product/brandsaver,/control/product/brandupdater,/control/product/typedeleter,/control/product/typefinder,/control/product/typesaver,/control/product/typeupdater,/control/upload/uploadfilesaver,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0,dataSource,org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0,entityManagerFactory,org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor#0,txManager,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor]; root of factory hierarchy com.itcast.service.product.BrandServiceImpl@10952e8 null 测试Find()方法! Tests run: 2, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 4.465 sec <<< FAILURE!Results :Tests in error: testfind(com.itcast.service.product.BrandServiceTest)Tests run: 7, Failures: 0, Errors: 1, Skipped: 0------------------------------------------------------------------------ BUILD FAILURE ------------------------------------------------------------------------ Total time: 8.499s Finished at: Sat Oct 08 20:37:56 CST 2011 Final Memory: 18M/254M ------------------------------------------------------------------------ Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.9:test (default-test) on project babasportbluetripe: There are test failures.Please refer to D:\MavenProjects\babasportrefactoring\babasportbluetripe\target\surefire-reports for the individual test results. -> [Help 1]To see the full stack trace of the errors, re-run Maven with the -e switch. Re-run Maven using the -X switch to enable full debug logging.For more information about the errors and possible solutions, please read the following articles: [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
beans.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <context:component-scan base-package="com.itcast"/>
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}"/>
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="${initialSize}" />
<!-- 连接池的最大值 -->
<property name="maxActive" value="${maxActive}"/>
<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="${maxIdle}"/>
<!-- 最小空闲值,当空闲的连接数少于阀值时,连接池就会去申请一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="${minIdle}" />
</bean>
<!-- This will ensure that hibernate or jpa exceptions are automatically translated into
Spring's generic DataAccessException hierarchy for those classes annotated with Repository
For example see PersonDaoJpa-->
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<!-- 类工厂由spring管理 -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/><!-- 注入数据源bean到实体管理工厂bean -->
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>
<property name="loadTimeWeaver"><!-- 运行时植入 -->
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
</property>
</bean>
<!-- bean post-processor for JPA annotations -->
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<!-- 事务由spring管理 -->
<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/><!-- 注入实体管理工厂bean到事务管理bean -->
</bean>
<tx:annotation-driven transaction-manager="txManager"/><!-- 事务声明方式是注解 -->
<!-- <bean id="producttypelist" class="com.itcast.web.action.product.ProductTypeAction" />--></beans>
是网络连接或数据库连接的问题吗
testSave(com.itcast.service.product.BrandServiceTest) Time elapsed: 0.002 sec <<< ERROR!
从这个错误来看,估计是时间超时问题!你可以仔细看看!
给一个完整代码下载链接吧:
完整下载:http://www.oschina.net/code/snippet_104958_6526
cd D:\MavenProjects\babasportrefactoring\babasportbluetripe; JAVA_HOME=D:\\program\\jdk1.6 D:\\program\\apache-maven-3.0.3\\bin\\mvn.bat -Dnetbeans.deploy=true -Dnetbeans.deploy.clientUrlPart=/ package
Scanning for projects...
------------------------------------------------------------------------
Building babasportbluetripe 1.0-SNAPSHOT
------------------------------------------------------------------------[resources:resources]
Using 'UTF-8' encoding to copy filtered resources.
Copying 24 resources[compiler:compile]
Compiling 27 source files to D:\MavenProjects\babasportrefactoring\babasportbluetripe\target\classes[resources:testResources]
Using 'UTF-8' encoding to copy filtered resources.
Copying 10 resources[compiler:testCompile]
Compiling 2 source files to D:\MavenProjects\babasportrefactoring\babasportbluetripe\target\test-classes[surefire:test]
Surefire report directory: D:\MavenProjects\babasportrefactoring\babasportbluetripe\target\surefire-reports-------------------------------------------------------
T E S T S
-------------------------------------------------------
Concurrency config is parallel='methods', perCoreThreadCount=true, threadCount=10, useUnlimitedThreads=false
2011-10-8 20:37:53 org.springframework.test.context.TestContextManager retrieveTestExecutionListeners
锟愶繀锟忥劲: @TestExecutionListeners is not present for class [class com.itcast.service.product.BrandServiceTest]: using defaults.
2011-10-8 20:37:53 org.springframework.test.context.TestContextManager retrieveTestExecutionListeners
锟愶繀锟忥劲: @TestExecutionListeners is not present for class [class com.itcast.service.product.TypeServiceTest]: using defaults.
Running com.itcast.service.product.TypeServiceTest
测试Find()方法!
2011-10-8 20:37:56 com.itcast.service.product.TypeServiceTest testFind
信息: 获取id为3的记录 com.itcast.model.product.Type@650646
测试Delete()方法!
测试Update()方法!
2011-10-8 20:37:56 com.itcast.service.product.TypeServiceTest testUpdate
信息: 更新 id为3的记录 com.itcast.model.product.Type@1b59919
当前产品类别名称为:6篮球产品;其id为 7
当前产品类别名称为:7篮球产品;其id为 8
当前产品类别名称为:8篮球产品;其id为 9
当前产品类别名称为:9篮球产品;其id为 10
当前产品类别名称为:10篮球产品;其id为 11
当前产品类别名称为:11篮球产品;其id为 12
当前产品类别名称为:12篮球产品;其id为 13
当前产品类别名称为:13篮球产品;其id为 14
当前产品类别名称为:14篮球产品;其id为 15
当前产品类别名称为:15篮球产品;其id为 16
测试save()方法!
Tests run: 5, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.22 sec
Running com.itcast.service.product.BrandServiceTest
2011-10-8 20:37:54 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans.xml]
2011-10-8 20:37:54 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.GenericApplicationContext@30e280: startup date [Sat Oct 08 20:37:54 CST 2011]; root of context hierarchy
2011-10-8 20:37:54 org.springframework.core.io.support.PropertiesLoaderSupport loadProperties
信息: Loading properties file from class path resource [jdbc.properties]
2011-10-8 20:37:54 org.springframework.context.support.AbstractApplicationContext$BeanPostProcessorChecker postProcessAfterInitialization
信息: Bean 'dataSource' of type [class org.apache.commons.dbcp.BasicDataSource] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2011-10-8 20:37:54 org.springframework.context.support.AbstractApplicationContext$BeanPostProcessorChecker postProcessAfterInitialization
信息: Bean 'org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver#39443f' of type [class org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2011-10-8 20:37:54 org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean createNativeEntityManagerFactory
信息: Building JPA container EntityManagerFactory for persistence unit 'itcast'
2011-10-8 20:37:55 org.springframework.context.support.AbstractApplicationContext$BeanPostProcessorChecker postProcessAfterInitialization
信息: Bean 'entityManagerFactory' of type [class org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2011-10-8 20:37:55 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@9cbd4b: defining beans [brandService,typeService,uploadFileService,/control/product/allbrandsfinder,/control/product/alltypesfinder,/control/product/branddeleter,/control/product/brandfinder,/control/product/brandsaver,/control/product/brandupdater,/control/product/typedeleter,/control/product/typefinder,/control/product/typesaver,/control/product/typeupdater,/control/upload/uploadfilesaver,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0,dataSource,org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0,entityManagerFactory,org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor#0,txManager,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor]; root of factory hierarchy
com.itcast.service.product.BrandServiceImpl@10952e8
null
测试Find()方法!
Tests run: 2, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 4.465 sec <<< FAILURE!Results :Tests in error:
testfind(com.itcast.service.product.BrandServiceTest)Tests run: 7, Failures: 0, Errors: 1, Skipped: 0------------------------------------------------------------------------
BUILD FAILURE
------------------------------------------------------------------------
Total time: 8.499s
Finished at: Sat Oct 08 20:37:56 CST 2011
Final Memory: 18M/254M
------------------------------------------------------------------------
Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.9:test (default-test) on project babasportbluetripe: There are test failures.Please refer to D:\MavenProjects\babasportrefactoring\babasportbluetripe\target\surefire-reports for the individual test results.
-> [Help 1]To see the full stack trace of the errors, re-run Maven with the -e switch.
Re-run Maven using the -X switch to enable full debug logging.For more information about the errors and possible solutions, please read the following articles:
[Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException