9
回答
用Spring的测试框架报错。
华为云数据库免费试用   

以下是报错代码,已经找到spring的配置文件了,但没有成功解析配置文件

java.lang.IllegalStateException: Failed to load ApplicationContext
	at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:201)
	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:255)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:111)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:148)
	at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
	at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
	at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
	at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
	at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:97)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class path resource [spring-base.xml]; nested exception is java.lang.ArrayIndexOutOfBoundsException: 27233
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:420)
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:342)
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:310)
	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)
	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)
	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)
	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:212)
	at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:81)
	at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:42)
	at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:173)
	at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:197)
	... 17 more
Caused by: java.lang.ArrayIndexOutOfBoundsException: 27233
	at org.springframework.asm.ClassReader.readUTF8(Unknown Source)
	at org.springframework.asm.ClassReader.accept(Unknown Source)
	at org.springframework.asm.ClassReader.accept(Unknown Source)
	at org.springframework.core.type.classreading.SimpleMetadataReader.getAnnotationMetadata(SimpleMetadataReader.java:55)
	at org.springframework.core.type.filter.AnnotationTypeFilter.matchSelf(AnnotationTypeFilter.java:68)
	at org.springframework.core.type.filter.AbstractTypeHierarchyTraversingFilter.match(AbstractTypeHierarchyTraversingFilter.java:55)
	at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.isCandidateComponent(ClassPathScanningCandidateComponentProvider.java:252)
	at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.findCandidateComponents(ClassPathScanningCandidateComponentProvider.java:192)
	at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:201)
	at org.springframework.context.annotation.ComponentScanBeanDefinitionParser.parse(ComponentScanBeanDefinitionParser.java:84)
	at org.springframework.beans.factory.xml.NamespaceHandlerSupport.parse(NamespaceHandlerSupport.java:69)
	at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1297)
	at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1287)
	at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:135)
	at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:92)
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:507)
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:398)
	... 27 more

以下是测试用例代码:

package com.demo;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;

import com.entity.demo.Customer;
import com.service.customer.CustomerService;

@ContextConfiguration(locations = { "/spring-base.xml" })
public class CustomerServiceTest extends AbstractTransactionalJUnit4SpringContextTests{
	@Autowired
	private CustomerService customerService;
	
	@Test
	public void testGetText() {
		Customer customer = new Customer();
		customer.setCustomerid("yuanp");
		customer.setPassword("123456");
		customer.setCustomername("袁培");
		customer.setGender("male");
		customer.setTelephone("88888888");
		customer.setProductid("4028818528c7d8f30128c7e5c7d40027");
		
		SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    Date date = new Date();
	    String time = df1.format(date);

		customer.setBegindate(Timestamp.valueOf(time));
		customer.setEnddate(Timestamp.valueOf(time+2));
		
		customerService.saveCustomer(customer, "jiangj");
	}

	public CustomerService getCustomerService() {
		return customerService;
	}

	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}

}

以下是spring配置文件:

<?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:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
	default-autowire="byName" default-lazy-init="true">
	<description>Spring公共配置文件</description>

	<!-- 使用annotation 自动注册bean,并检查@Required,@Autowired的属性已被注入 -->
	<context:component-scan base-package="com"/>
	<!-- 环境变量 -->
	<context:property-placeholder location="classpath:webconfig.properties"/>
<!-- 数据源配置,主要用于开发测试环境 -->
	<!--JNDI DataSource for J2EE environments-->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
	        <property name="driverClass">
	            <value>${driverClass}</value>
	        </property>
	        <property name="jdbcUrl">
	            <value>${jdbcUrl}</value>
	        </property>
	        <property name="user">
	            <value>${user}</value>
	        </property>
	        <property name="password">
	            <value>${password}</value>
	        </property>
	 <!--连接池中保留的最小连接数。-->
	        <property name="minPoolSize">
	            <value>50</value>
	        </property>
	 <!--连接池中保留的最大连接数。Default: 15 -->
	        <property name="maxPoolSize">
	            <value>500</value>
	        </property>
	<!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
	        <property name="initialPoolSize">
	            <value>10</value>
	        </property>
	 <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
	        <property name="maxIdleTime">
	            <value>60</value>
	        </property>
	 <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
	        <property name="acquireIncrement">
	            <value>5</value>
	        </property>
	 <!--JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements
	  属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。
	  如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0-->
	        <property name="maxStatements">
	            <value>0</value>
	        </property>
	 <!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
	        <property name="idleConnectionTestPeriod">
	            <value>60</value>
	        </property>
	 <!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
	        <property name="acquireRetryAttempts">
	            <value>30</value>
	        </property>
	 <!--获取连接失败将会引起所有等待连接池来获取连接的线程抛出异常。但是数据源仍有效
	  保留,并在下次调用getConnection()的时候继续尝试获取连接。如果设为true,那么在尝试
	  获取连接失败后该数据源将申明已断开并永久关闭。Default: false-->
	        <property name="breakAfterAcquireFailure">
	            <value>true</value>
	        </property>
	 <!--因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的
	  时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable
	  等方法来提升连接测试的性能。Default: false
-->
	        <property name="testConnectionOnCheckout">
	            <value>false</value>
	        </property>
	</bean>

	<!-- Hibernate配置 -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="namingStrategy"><bean class="org.hibernate.cfg.ImprovedNamingStrategy"/></property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">${dialect}</prop>
				<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
				<prop key="hibernate.cache.provider_configuration_file_resource_path">/ehcache.xml</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop><!-- 第一次使用的时候用create,其他为update-->
				<!-- 检索
				<prop key="hibernate.search.default.directory_provider">org.hibernate.search.store.FSDirectoryProvider</prop>
				<prop key="hibernate.search.default.indexBase">/lucene/indexes</prop>
				 -->
			</props>
		</property>
		<property name="annotatedClasses">
			<list>
				<value>com.security.domain.Resource</value>
				<value>com.security.domain.Role</value>
				<value>com.security.domain.User</value>
			</list>
		</property>
		<property name="packagesToScan">
			<list>
				<value>com.entity.*</value>
			</list>
		</property>
		<property name="lobHandler" ref="defaultLobHandler"/>
	</bean>
	
	<bean id="defaultLobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler"/>

	<!-- 事务配置 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<!-- 使用annotation定义事务 -->
	<tx:annotation-driven transaction-manager="transactionManager" />

	<bean id="dbinit" class="com.service.InitSystemData" init-method="init" lazy-init="false">
		<property name="run" value="false"/><!-- 第一次使用的时候用true,其他为false-->
	</bean>
	
</beans>

我启动项目的时候是没有错误的,就是用spring的测试框架的时候它报错,真不知道怎么办啊。

举报
季玉
发帖于8年前 9回/14K+阅
顶部