亚洲好骚综合-亚洲黄色录像-亚洲黄色网址-亚洲黄色网址大全-99久久99久久-99久久99久久精品国产

您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 > junit
Maven下使用Junit對Spring進行單元測試
作者:Kinsang 發布時間:[ 2017/5/18 13:38:40 ] 推薦標簽:Junit 單元測試

  主要步驟
  1. 在工程的pom文件中增加spring-test的依賴:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
  2. 使用springframework提供的單元測試
  新建測試類,并在該類上加上兩個注解:
  @RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations={"classpath*:ApplicationContext.xml"})
  @RunWith 大家并不陌生,junit4里用它來做junit加載器
  @ContextConfiguration 主要用來加載spring的配置文件路徑:是一個字符串數組,可以加載多個spring配置文件
  測試代碼如下:
1 import static org.junit.Assert.assertEquals;
2
3 import org.junit.Test;
4 import org.junit.runner.RunWith;
5 import org.springframework.beans.factory.annotation.Autowired;
6 import org.springframework.context.ApplicationContext;
7 import org.springframework.test.context.ContextConfiguration;
8 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
9
10 @RunWith(SpringJUnit4ClassRunner.class)
11 @ContextConfiguration(locations = {"classpath:ApplicationContext.xml"})
12 public class EmpolyeeTest {
13     @Autowired
14     ApplicationContext ctx;
15
16     @Test
17     public void testEmployee(){
18         Employee employee =(Employee) ctx.getBean("employee");
19         assertEquals("zhangsan",employee.getName());
20     }
21
22 }
  3. 封裝基于AbstractJUnit4SpringContextTests的測試基類
1 import org.junit.Test;
2 import org.junit.runner.RunWith;
3 import org.springframework.context.ApplicationContext;
4 import org.springframework.test.context.ContextConfiguration;
5 import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
6 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
7
8 @RunWith(SpringJUnit4ClassRunner.class)
9 @ContextConfiguration(locations={"classpath*:ApplicationContext.xml"})
10 public class SpringTest extends AbstractJUnit4SpringContextTests {
11
12
13    public <T> T getBean(Class<T> type){
14        return applicationContext.getBean(type);
15    }
16
17    public Object getBean(String beanName){
18        return applicationContext.getBean(beanName);
19    }
20    protected ApplicationContext getContext(){
21        return applicationContext;
22    }
  然后其他測試類只需要繼承該類即可,可以省去每次都要綁定Application對象。
  4. 當項目變得復雜,其中的spring配置文件被拆分成了多個,這樣該如何引入多個配置文件呢?如下:
  @RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations = { "classpath*:spring-ctx-service.xml", "classpath*:spring-ctx-dao.xml" })
  這樣可以輕松的引入多個spring的配置文件了。或者配置符合某一個正則表達式的一類文件,如:
  1 @RunWith(SpringJUnit4ClassRunner.class)
  2 @ContextConfiguration(locations = "classpath*:spring-ctx-*.xml")

軟件測試工具 | 聯系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網站地圖
滬ICP備07036474 2003-2017 版權所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd
主站蜘蛛池模板: 一级视频黄色 | 99在线免费播放 | 成人免费视频无遮挡在线看 | 日韩不卡视频在线观看 | 杏色国产成人精品视频 | 99在线观看视频免费精品9 | 一级日韩 | 国产无遮挡又爽又色又刺激 | 欧美一区二区三区高清视频 | 欧美午夜久久 | 日韩欧免费一区二区三区 | 欧美韩日| 欧美另类亚洲 | 成人小视频免费 | 国产一区二区免费播放 | 九月婷婷人人澡人人添人人爽 | 久久久久免费精品国产 | 国产丝袜美女 | 午夜想想爱午夜剧场 | 日日摸夜夜摸狠狠摸97 | 天天综合天天看夜夜添狠狠玩 | 国产亚洲精品精品国产亚洲综合 | 黄色一级视频播放 | 色综合天天综一个色天天综合网 | 久久久高清日本道免费观看 | 国产亚洲人成网站在线观看不卡 | 亚洲精品无码不卡在线播放he | 欧美日韩中文字幕在线手机版本 | 免费观看成人www精品视频在线 | 日本特交大片免费观看 | xxxx日本在线播放免费不卡 | 国产高清免费的视频 | 欧美一级www片免费观看 | 久草久草久草久草 | 黄色大片免费播放 | 中文字幕日本久久2019 | 亚洲精品专区 | 韩国午夜理伦三级在线观看仙踪林 | 日本中文在线视频 | 欧美亚洲精品在线 | 亚洲黄色在线观看视频 |