【junit】junit4单元测试eclipse
這兩天看了網上down下來的junit視頻,有百度了一下junit。
一、百度junit,在github上發布的都是5了,沒有看到4的影子了,我在eclipse的maven加載了最新的5.0.2,發現效果并不理想。
不理想原因:1、可能自己基礎太差,不會用;
2、可能是我的eclipse不支持junit5(我也不知道),反正建立測試文件的時候,eclipse只能選擇3或者4;
二、視頻中推薦組合:junit4.11、hamcrest-core-1.3、hamcrest-library-1.3,maven引入jar包,如圖
三、創建測試類:
說明:1、eclipse提供版本3、版本4,選擇版本4;
2、為那個類文件建立測試類,就在該類文件名后加Test;
3、最下面選擇對應的目標類文件,下一步之后選擇該類文件中具體的類方法,下面是我為T1、T2文件建立的測試文件,如圖
四、測試類方法說明
public class T2Test {
@BeforeClass//該測試文件只打印一次
public static void BeforeClass() {
System.out.println("第一個beforeClass");
}
@AfterClass//該測試文件只打印一次
public static void AfterClass() {
System.out.println("第一個afterClass");
}
@Before//每個方法前只打印一次
public void Before() {
System.out.println("第一個before");
}
@After//每個方法后只打印一次
public void After() {
System.out.println("第一個after");
}
@Test
public void testStrShow() {
String str = new T2().strShow();
String st = "dd";
System.out.println(st);
assertThat(str, is("hello"));
assertThat(str, not("hellot"));
assertThat(str, containsString("he"));
assertThat(str, endsWith("llo"));
assertThat(str, startsWith("he"));
assertThat(5, equalTo(str.length()));
assertThat(str, equalToIgnoringCase("hEllo"));
assertThat(st, equalToIgnoringWhiteSpace(" ? ?dd "));//忽略開頭、結尾的空格
}
@Test
public void testMap() {
Map<String, Object> map = new T2().map();
List<String> list = new ArrayList<>();
list.add("id");
System.out.println("map");
assertThat(map, hasEntry("id", "wg"));
assertThat(list, hasItem("id"));
assertThat(map, hasKey("id"));
assertThat(map, hasValue("wg"));
}
}
五、運行
分為三種:1、單個類方法進行測試,在工程目錄測試類文件下的測試類方法右鍵,測試單個方法;
2、測試整個類文件,在目錄下測試文件下右鍵,或者在打開的類文件下右鍵;
3、測試多個類文件,在目錄下,對整個測試文件夾右鍵,下面的測試文件都進行測試
轉載于:https://www.cnblogs.com/gang130532/p/8276153.html
總結
以上是生活随笔為你收集整理的【junit】junit4单元测试eclipse的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一行代码搞定 FTP 服务
- 下一篇: Gitlab搭建安装及使用中遇到的问题。