使用junit進行介面測試的幾個知識點

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.