JUnit5使用教程及简单的测试案例(Idea,Android studio)
生活随笔
收集整理的這篇文章主要介紹了
JUnit5使用教程及简单的测试案例(Idea,Android studio)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一.介紹
什么是Junit5 ?
先看來個公式:
JUnit 5 =?JUnit Platform?+?JUnit Jupiter?+?JUnit Vintage
這看上去比Junit4?復雜,實際上在導入包時也會復雜一些。
JUnit Platform是在JVM上啟動測試框架的基礎。
JUnit Jupiter是JUnit5擴展的新的編程模型和擴展模型,用來編寫測試用例。Jupiter子項目為在平臺上運行Jupiter的測試提供了一個TestEngine?(測試引擎)。
JUnit Vintage提供了一個在平臺上運行JUnit 3和JUnit 4的TestEngine?。
二.引入
對于JUnit有多種引入,這里介紹Maven和Gradle兩種方式
1.Maven(pom.xml)
<dependencies> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-launcher</artifactId> <version>1.0.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.0.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> <version>4.12.1</version> <scope>test</scope> </dependency> </dependencies>2.Gradle(app.gradle)
dependencies {testCompile('org.junit.jupiter:junit-jupiter-api:5.2.0')testRuntime('org.junit.jupiter:junit-jupiter-engine:5.2.0') }三.基本方法及注解
四.使用實例
下面我用一個檢查手機號碼的工具類PhoneNumberCheckUtils來進行簡單的測試.
package dream.anywhere.test; /** * Created by ljb on 2018-05-20. */ import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; public class PhoneNumberCheckUtils {/** * 大陸號碼或香港號碼均可 */ public static boolean isPhoneLegal(String str)throws PatternSyntaxException {return isChinaPhoneLegal(str) || isHKPhoneLegal(str); }/** * 大陸手機號碼11位數,匹配格式:前三位固定格式+后8位任意數 * 此方法中前三位格式有: * 13+任意數 * 15+除4的任意數 * 18+除1和4的任意數 * 17+除9的任意數 * 147 */ public static boolean isChinaPhoneLegal(String str) throws PatternSyntaxException {String regExp = "^((13[0-9])|(15[^4])|(18[0,2,3,5-9])|(17[0-8])|(147))\\d{8}$"; Pattern p = Pattern.compile(regExp); Matcher m = p.matcher(str); return m.matches(); }/** * 香港手機號碼8位數,5|6|8|9開頭+7位任意數 */ public static boolean isHKPhoneLegal(String str)throws PatternSyntaxException {String regExp = "^(5|6|8|9)\\d{7}$"; Pattern p = Pattern.compile(regExp); Matcher m = p.matcher(str); return m.matches(); } } 然后在代碼框右鍵然后選擇 Go to 然后選擇Test,然后create new test然后OK
現在得到了一個簡單的測試類,然后補充一下,寫幾個測試方法
package dream.anywhere.test; import org.junit.Test; import static org.junit.Assert.*; /** * Created by 94829 on 2018-05-20. */ public class PhoneNumberTest {String phoneNumber1 = "15229235980"; String phoneNumber2 = "15256555459801111"; String phoneNumber3 = "83749658"; @Test public void isPhoneLegal() throws Exception {assertTrue(PhoneNumberCheckUtils.isPhoneLegal(phoneNumber1)); }@Test public void isChinaPhoneLegal() throws Exception {assertTrue(PhoneNumberCheckUtils.isChinaPhoneLegal(phoneNumber2)); }@Test public void isHKPhoneLegal() throws Exception {assertTrue(PhoneNumberCheckUtils.isHKPhoneLegal(phoneNumber3)); }}右鍵Run這個測試類
可以看到,三個方法中有兩個方法通過,一個方法未通過,右邊給出了未通過方法的出錯行
最后附上JUnit5官方文檔以及官方案例的地址
文檔: ??JUnit 5 doc
案例: ??JUnit5-samples
總結
以上是生活随笔為你收集整理的JUnit5使用教程及简单的测试案例(Idea,Android studio)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 华为首推一体机,将在产品中融入元宇宙相
- 下一篇: EPLAN 软件平台中的词“点“大全