1 加载被测接口
@Resource
public xxxxService xxxxService;
使用spring的resource标签,直接将bean文件中的接口id进行加载,可使用简单的测试方法测试是否服务注入成功
如:
public void test(){
system.out.println(xxxxService == null);
}
2 spring原生的加载bean方式
ApplicationContext context=new ClassPathXmlApplicationContext("TestApplicationContext.xml");
xxxxService xxxxService =context.getBean("xxxxService");
3 接口测试的数据准备
1)直接进行db操作
1.1 jdbc直接链接
1.2 配置bean,进行文件加载
datasource文件:
<?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:p="http://www.springframework.org/schema/p"xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:property-placeholderlocation="classpath:dbcpconfig.properties" />
<bean id="xxxx"class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<propertyname="driverClassName"value="${driverClassName}" />
<property name="url" value="${url}" />
<propertyname="username"value="${username}" />
<propertyname="password"value="${password}" />
<propertyname="removeAbandoned">
<value>true</value>
</property>
<propertyname="removeAbandonedTimeout">
<value>180</value>
</property>
<propertyname="logAbandoned">
<value>true</value>
</property>
<propertyname="validationQuery">
<value>select 1</value>
</property>
<!-- 连接池启动时的初始值 -->
<propertyname="initialSize"value="${initialSize}" />
<!-- 连接池的最大值 -->
<propertyname="maxActive"value="${maxActive}" />
<!--最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<propertyname="maxIdle"value="${maxIdle}" />
<!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<propertyname="minIdle"value="${minIdle}" />
</bean>
</beans>
相关dbproperties文件:#连接设置
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://
username=
password=
url_review=jdbc:mysql://
username_review=
password_review=
url_life=jdbc:mysql://
username_life=
password_life=
#<!-- 初始化连接 -->
initialSize=10
#最大连接数量
maxActive=500
#<!-- 最大空闲连接 -->
maxIdle=20
#<!-- 最小空闲连接 -->
minIdle=5
#<!-- 超时等待时间以毫秒为单位 6000毫秒/1000等于60秒 -->
maxWait=60000
#JDBC驱动建立连接时附带的连接属性属性的格式必须为这样:[属性名=property;]
#注意:"user" 与 "password" 两个属性会被明确地传递,因此这里不需要包含他们。
connectionProperties=useUnicode=true&characterEncoding=gbk
#指定由连接池所创建的连接的自动提交(auto-commit)状态。
defaultAutoCommit=true
#driver default 指定由连接池所创建的连接的只读(read-only)状态。
#如果没有设置该值,则“setReadOnly”方法将不被调用。(某些驱动并不支持只读模式,如:Informix)
defaultReadOnly=
#driver default 指定由连接池所创建的连接的事务级别(TransactionIsolation)。
#可用值为下列之一:(详情可见javadoc。)NONE,READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE
defaultTransactionIsolation=READ_UNCOMMITTED
2)调用相关服务
直接根据所需服务,进行注入,来进行调用,制造测试数据
4 设置eclipse的运行内存
在工程,右键,run configurations=》
右侧选择Arguguments
vm argument 设置
-Xms4096m -Xmx4096m -XX:PermSize=4096m -XX:MaxPermSize=4096m