使用junit进行接口测试的几个知识点

本文介绍了如何在JUnit中进行接口测试,包括通过@Resource注解加载bean、使用ApplicationContext获取bean,以及接口测试的数据准备,如直接使用jdbc操作数据库和调用相关服务来制造测试数据。同时,提到了调整Eclipse运行配置以优化内存设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值