日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring学习笔记06-spring整合junit(出现的问题,解决的思路)

發(fā)布時間:2024/7/19 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring学习笔记06-spring整合junit(出现的问题,解决的思路) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

spring學(xué)習(xí)筆記06-spring整合junit(出現(xiàn)的問題,解決的思路)

文章目錄

  • spring學(xué)習(xí)筆記06-spring整合junit(出現(xiàn)的問題,解決的思路)
    • 3.1測試類中的問題和解決思路
      • 3.1.1 問題
      • 3.2.1 第一步:pom.xml文件導(dǎo)入相關(guān)的依賴
      • 3.2.2 第二步:使用@RunWith 注解替換原有運行器
      • 3.2.4 第四步:使用@Autowired 給測試類中的變量注入數(shù)據(jù)
    • 3.3為什么不把測試類配到 xml 中

3.1測試類中的問題和解決思路

3.1.1 問題

??在測試類中,每個測試方法都有以下兩行代碼:

ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); IAccountService as = ac.getBean("accountService",IAccountService.class);

??這兩行代碼的作用是獲取容器,如果不寫的話,直接會提示空指針異常。所以又不能輕易刪掉。

3.1.2 解決思路分析
??針對上述問題,我們需要的是程序能自動幫我們創(chuàng)建容器。一旦程序能自動為我們創(chuàng)建 spring 容器,我們就無須手動創(chuàng)建了,問題也就解決了。
??我們都知道,junit 單元測試的原理(在 web 階段課程中講過),但顯然,junit 是無法實現(xiàn)的,因為它自己都無法知曉我們是否使用了 spring 框架,更不用說幫我們創(chuàng)建 spring 容器了。不過好在,junit 給我們暴露了一個注解,可以讓我們替換掉它的運行器。
??這時,我們需要依靠 spring 框架,因為它提供了一個運行器,可以讀取配置文件(或注解)來創(chuàng)建容器。我們只需要告訴它配置文件在哪就行了。

3.2.1 第一步:pom.xml文件導(dǎo)入相關(guān)的依賴

<dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.0.2.RELEASE</version></dependency><!-- spring-test--> <dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.0.2.RELEASE</version></dependency><dependency><groupId>commons-dbutils</groupId><artifactId>commons-dbutils</artifactId><version>1.4</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.6</version></dependency><dependency><groupId>c3p0</groupId><artifactId>c3p0</artifactId><version>0.9.1.2</version></dependency><!-- junit--> <dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency></dependencies>

3.2.2 第二步:使用@RunWith 注解替換原有運行器

/** * 測試類 * @Version 1.0 */ @RunWith(SpringJUnit4ClassRunner.class)public class AccountServiceTest { }

3.2.3 第三步:使用@ContextConfiguration 指定 spring 配置文件的位置

/** * 測試類 * @author 黑馬程序員 * @Company http://www.ithiema.com * @Version 1.0 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations= {"classpath:bean.xml"})public class AccountServiceTest { }

此時的bean.xml位于resources目錄下:

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!--配置代理的service--><bean id="proxyAccountService" factory-bean="beanFactory" factory-method="getAccountService"></bean><!--配置beanfactory--><bean id="beanFactory" class="com.itheima.factory.BeanFactory"><!-- 注入service --><property name="accountService" ref="accountService"></property><!-- 注入事務(wù)管理器 --><property name="txManager" ref="txManager"></property></bean><!-- 配置Service --><bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"><!-- 注入dao --><property name="accountDao" ref="accountDao"></property></bean><!--配置Dao對象--><bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl"><!-- 注入QueryRunner --><property name="runner" ref="runner"></property><!-- 注入ConnectionUtils --><property name="connectionUtils" ref="connectionUtils"></property></bean><!--配置QueryRunner--><bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype"></bean><!-- 配置數(shù)據(jù)源 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><!--連接數(shù)據(jù)庫的必備信息--><property name="driverClass" value="com.mysql.jdbc.Driver"></property><property name="jdbcUrl" value="jdbc:mysql://localhost:3306/eesy"></property><property name="user" value="root"></property><property name="password" value="1234"></property></bean><!-- 配置Connection的工具類 ConnectionUtils --><bean id="connectionUtils" class="com.itheima.utils.ConnectionUtils"><!-- 注入數(shù)據(jù)源--><property name="dataSource" ref="dataSource"></property></bean><!-- 配置事務(wù)管理器--><bean id="txManager" class="com.itheima.utils.TransactionManager"><!-- 注入ConnectionUtils --><property name="connectionUtils" ref="connectionUtils"></property></bean> </beans>

@ContextConfiguration 注解:
??locations 屬性:用于指定配置文件的位置。如果是類路徑下,需要用 classpath:表明
??classes 屬性:用于指定注解的類。當(dāng)不使用 xml 配置時,需要用此屬性指定注解類的位置。

3.2.4 第四步:使用@Autowired 給測試類中的變量注入數(shù)據(jù)

/** * 測試類 * @Version 1.0 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations= {"classpath:bean.xml"}) public class AccountServiceTest { @Autowired private IAccountService as ; }

3.3為什么不把測試類配到 xml 中

??在解釋這個問題之前,先解除大家的疑慮,配到 XML 中能不能用呢?
答案是肯定的,沒問題,可以使用。
??那么為什么不采用配置到 xml 中的方式呢?
這個原因是這樣的:
??第一:當(dāng)我們在 xml 中配置了一個 bean,spring 加載配置文件創(chuàng)建容器時,就會創(chuàng)建對象。
??第二:測試類只是我們在測試功能時使用,而在項目中它并不參與程序邏輯,也不會解決需求上的問題,所以創(chuàng)建完了,并沒有使用。那么存在容器中就會造成資源的浪費。
所以,基于以上兩點,我們不應(yīng)該把測試配置到 xml 文件中

總結(jié)

以上是生活随笔為你收集整理的spring学习笔记06-spring整合junit(出现的问题,解决的思路)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。