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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

@RunWith的使用

發布時間:2024/3/13 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 @RunWith的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

    • @RunWith作用
    • @RunWith(SpringJunit4ClassRunner.class) 使用說明
    • 測試@ActiveProfiles("dev")

@RunWith作用

  • @RunWith 就是一個運行器

  • @RunWith(JUnit4.class) 就是指用JUnit4來運行

  • @RunWith(SpringJUnit4ClassRunner.class),讓測試運行于Spring測試環境

  • @RunWith(Suite.class) 的話就是一套測試集合,

  • @ContextConfiguration Spring整合JUnit4測試時,使用注解引入多個配置文件

單個文件
@ContextConfiguration(Locations=“classpath:applicationContext.xml”)
@ContextConfiguration(classes = SimpleConfiguration.class)

多個文件時,可用{}
@ContextConfiguration(locations = { “classpath:spring1.xml”, “classpath:spring2.xml” })

@RunWith(SpringJunit4ClassRunner.class) 使用說明

轉載:https://blog.csdn.net/u013803262/article/details/52253825

今天在看Spring的Demo的時候,看到了如此單元測試的寫法

如下:

@RunWIth(SpringJunit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:applicationContext.xml"} public class MyTest {@Testpublic void hehe(){//.......} }

這種寫法是為了讓測試在Spring容器環境下執行。
Spring的容器環境是啥呢?
比如常見的 Service Dao Action , 這些個東西,都在Spring容器里,junit需要將他們拿到,并且使用來測試。
好,筆者寫一個十分簡單的demo讓大家有個體會!
顯示demo的項目結構:
要寫的東西就兩個 applicationContext.xml 和MyTest.java

applicationContext.xml 中僅僅只定義了一個Date對象。
接下來是MyTest.java的內容:
最后只需要運行就可以了。

最后如圖所示,成功拿到了Spring容器里的Date對象。
諸如哪些 Action Service Dao ServiceImpl DaoImpl都是一個道理,可以通過這種方式拿到,然后進行單元測試。。。

測試@ActiveProfiles(“dev”)

package com.ssm.chapter10.test; import javax.sql.DataSource; /*******imports******/ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes=ProfileConfig.class) @ActiveProfiles("dev") public class ProfileTest{@Autowiredprivate DataSource dataSource;@Testpublic void test(){System.out.println(dataSource.getClass().getName());} }

@RunWith作用 - weixin_41315469的博客 - CSDN博客
https://blog.csdn.net/weixin_41315469/article/details/79675044
@RunWith(SpringJunit4ClassRunner.class) 使用說明 - 碼農在路上 - 博客園
https://www.cnblogs.com/lzmrex/articles/9236977.html

總結

以上是生活随笔為你收集整理的@RunWith的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。