我正在尝试运行TestNG + Spring + JPA + Hibernate'Hello World'测试。执行结果为NullPointerException,tiles configurer bean失败。
例外:
2011-08-20 18:12:18,547 [main] INFO org.springframework.web.servlet.view.tiles2.TilesConfigurer - TilesConfigurer: adding definitions [/WEB-INF/tiles/tiles.xml] 2011-08-20 18:12:18,552 [main] INFO org.springframework.beans.factory.support.DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@68cb6b: defining beans [org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler#0,org.springframework.security.access.method.DelegatingMethodSecurityMetadataSource#0,org.springframework.security.access.vote.AffirmativeBased#0,org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor#0,org.springframework.security.methodSecurityMetadataSourceAdvisor,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0,org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#0,nethawalaDS,jspViewResolver,tilesViewResolver,tilesConfigurer,nethawalaEMF,transactionManager,jpaVendorAdapter,validator,auditLog,org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor#0,logoutHandlerBean,userDetailsService,customAuthenticationFailureHandlerBean,customAuthenticationSuccessHandlerBean,httpSessionEventListener,org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0,org.springframework.format.support.FormattingConversionServiceFactoryBean#0,org.springframework.validation.beanvalidation.LocalValidatorFactoryBean#0,org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter#0,org.springframework.web.servlet.handler.MappedInterceptor#0,org.springframework.scheduling.annotation.internalAsyncAnnotationProcessor,org.springframework.scheduling.annotation.internalScheduledAnnotationProcessor,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,homeController,userSessionController,NHUserDAOImpl,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,nethawalaProperties,placeholderConfig]; root of factory hierarchy 2011-08-20 18:12:18,563 [main] ERROR org.springframework.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@fdfc58] to prepare test instance [com.tigermindz.test.FacultyDaoTest@1afae45] java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:308) at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109) at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75) at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321) at org.springframework.test.context.testng.AbstractTestNGSpringContextTests.springTestContextPrepareTestInstance(AbstractTestNGSpringContextTests.java:133) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:76) at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:525) at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:202) at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:130) at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:173) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:105) at org.testng.TestRunner.runWorkers(TestRunner.java:1147) at org.testng.TestRunner.privateRun(TestRunner.java:749) at org.testng.TestRunner.run(TestRunner.java:600) at org.testng.SuiteRunner.runTest(SuiteRunner.java:317) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:312) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:274) at org.testng.SuiteRunner.run(SuiteRunner.java:223) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1039) at org.testng.TestNG.runSuitesLocally(TestNG.java:964) at org.testng.TestNG.run(TestNG.java:900) at org.testng.TestNG.privateMain(TestNG.java:1182) at org.testng.TestNG.main(TestNG.java:1146) Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tilesConfigurer' defined in class path resource [nethawala-servlet.xml]: Invocation of init method failed; nested exception is java.lang.NullPointerException at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1420) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425) at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:84) at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:1) at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:280) at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:304) ... 28 more Caused by: java.lang.NullPointerException at org.springframework.web.servlet.view.tiles2.SpringTilesApplicationContextFactory$SpringWildcardServletTilesApplicationContext.(SpringTilesApplicationContextFactory.java:72) at org.springframework.web.servlet.view.tiles2.SpringTilesApplicationContextFactory.createApplicationContext(SpringTilesApplicationContextFactory.java:55) at org.springframework.web.servlet.view.tiles2.TilesConfigurer.afterPropertiesSet(TilesConfigurer.java:335) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417) ... 41 more
我的applicationContext.xml
,只显示了瓷砖配置bean def:
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles/tiles.xml</value>
</list>
</property>
</bean>
有很多谷歌搜索,只有事情发现,人们提到cglib和/或asm jar与spring和hibernate依赖项冲突。我不认为这适用于此,但我的lib文件夹有以下jar,抱歉不按顺序:
./tiles-template-2.2.2.jar ./spring-security-acl-3.0.5.RELEASE.jar ./org.springframework.context-3.0.5.RELEASE.jar ./hibernate-jpa-2.0-api-1.0.0.Final.jar ./tiles-api-2.2.2.jar ./validation-api-1.0.0.GA.jar ./org.springframework.web.servlet-3.0.5.RELEASE.jar ./hsqldb.jar ./spring-security-taglibs-3.0.5.RELEASE.jar ./commons-pool-1.5.6.jar ./aspectjrt-1.6.8.jar ./commons-beanutils-1.8.3.jar ./org.springframework.aop-3.0.5.RELEASE.jar ./spring-security-core-3.0.5.RELEASE.jar ./commons-collections-3.1.jar ./org.springframework.web-3.0.5.RELEASE.jar ./jstl-1.2.jar ./org.springframework.expression-3.0.5.RELEASE.jar ./dom4j-1.6.1.jar ./org.springframework.orm-3.0.5.RELEASE.jar ./log4j-1.2.16.jar ./org.springframework.beans-3.0.5.RELEASE.jar ./tiles-servlet-2.2.2.jar ./poi-3.7-20101029.jar ./spring-security-aspects-3.0.5.RELEASE.jar ./slf4j-api-1.6.1.jar ./aopalliance-1.0.jar ./slf4j-simple-1.6.1.jar ./hibernate3.jar ./slf4j-ext-1.6.1.jar ./spring-security-web-3.0.5.RELEASE.jar ./jta-1.1.jar ./asm-3.3.1.jar ./org.springframework.test-3.0.5.RELEASE.jar ./spring-security-config-3.0.5.RELEASE.jar ./org.springframework.core-3.0.5.RELEASE.jar ./org.springframework.asm-3.0.5.RELEASE.jar ./tiles-jsp-2.2.2.jar ./testng-6.0.1.jar ./commons-digester-2.1.jar ./commons-logging-1.1.1.jar ./org.springframework.transaction-3.0.5.RELEASE.jar ./javax.inject.jar ./commons-dbcp-1.4.jar ./slf4j-log4j12-1.6.1.jar ./hibernate-validator-4.1.0.Final.jar ./javassist-3.12.0.GA.jar ./tiles-core-2.2.2.jar ./aspectjweaver-1.6.8.jar ./org.springframework.jdbc-3.0.5.RELEASE.jar ./jackson-all-1.7.5.jar ./jstl-api-1.2.jar ./antlr-2.7.6.jar ./cglib-2.2.jar
测试类如下:
@ContextConfiguration(locations = { "classpath:test-servlet.xml" })
public class FacultyDaoTest extends AbstractTransactionalTestNGSpringContextTests {
private final XLogger logger = XLoggerFactory.getXLogger(FacultyDaoTest.class);
@Inject
FacultyDAO facultyDao;
@BeforeClass
public void setUp() {
logger.entry();
System.out.println("Inside setup");
}
@Test(groups = { "fast" })
@Rollback(value = false)
public void createFaculty() {
Faculty f = new Faculty();
f.setFirstName("John");
f.setLastName("Frost");
f.setCpsoNumber("23198");
f = facultyDao.makePersistent(f);
assert(f.getId() > 0);
}
@Test(groups = { "fast" })
public void aFastTest() {
logger.entry();
logger.info("Inside a Fast test");
}
@Test(groups = { "slow" })
public void aSlowTest() {
logger.entry();
logger.info("Inside a Slow test");
}
}
任何指针都将非常感激。
答案 0 :(得分:4)
您的测试加载了特定于Web应用程序的配置文件,但您的测试环境未提供所需的文件:
您需要将Web应用程序特定的配置与不适用于Web应用程序上下文的配置分开。
然后使用没有Web应用程序的测试。
对于您的意思是:删除测试配置的Tiles配置。因为在测试中/WEB-INF/tiles/tiles.xml
文件不可用。