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

您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 > junit
Junit使用及其原理分析
作者:alighters 發布時間:[ 2016/9/13 10:53:28 ] 推薦標簽:單元測試 Junit

  引入
  在 build.gradle 文件中
  dependencies {
  testCompile 'junit:junit:4.12'
  }
  這其中會引入兩個jar:junit-4.12.jar 和 hamcrest-core-1.3.jar
  介紹
  junit 中兩個重要的類 Assume 和 Assert, 以及其他一些重要的注解:BeforeClass,AfterClass,After,Before 及 Test,Ignore。
  其中,BeforeClass 和 AfterClass 在每個類加載的開始和結束時運行,需要設置 static 方法;而 Before和After 則是在每個測試方法的開始之前和結束之后運行。
  在 hamcrest-core 的 jar 包中,在 org.hamcrest.core 包中提供了一系列操作運算封裝,使測試代碼更加地易讀。如is,not,allOf,anyOf等。
  代碼示例
@Test
public void testAssertArrayEquals() {
byte[] expected = "trial".getBytes();
byte[] actual = "trial".getBytes();
assertArrayEquals("failure - byte arrays not same", expected, actual);
}
@Test
public void testAssertEquals() {
assertEquals("failure - strings are not equal", "text", "text");
}
@Test
public void testAssertFalse() {
assertFalse("failure - should be false", false);
}
@Test
public void testAssertNotNull() {
assertNotNull("should not be null", new Object());
}
@Test
public void testAssertNotSame() {
assertNotSame("should not be same Object", new Object(), new Object());
}
@Test
public void testAssertNull() {
assertNull("should be null", null);
}
@Test
public void testAssertSame() {
Integer aNumber = Integer.valueOf(768);
assertSame("should be same", aNumber, aNumber);
}
@Test
public void testAssertTrue() {
assertTrue("failure - should be true", true);
}
  以上代碼來自官方介紹的 Demo , 列舉的是常用而又基礎的操作,但遇到復雜的集合判斷操作,力不從心了,不過 Junit 提供了另一更為強大的 assertThat 方法,首先來看看它的使用:
// JUnit Matchers assertThat
@Test
public void testAssertThatBothContainsString() {
assertThat("albumen", both(containsString("a")).and(containsString("b")));
}
@Test
public void testAssertThatHasItems() {
assertThat(Arrays.asList("one", "two", "three"), hasItems("one", "three"));
}
@Test
public void testAssertThatEveryItemContainsString() {
assertThat(Arrays.asList(new String[] { "fun", "ban", "net" }), everyItem(containsString("n")));
}
// Core Hamcrest Matchers with assertThat
@Test
public void testAssertThatHamcrestCoreMatchers() {
assertThat("good", allOf(equalTo("good"), startsWith("good")));
assertThat("good", not(allOf(equalTo("bad"), equalTo("good"))));
assertThat("good", anyOf(equalTo("bad"), equalTo("good")));
assertThat(7, not(CombinableMatcher.<Integer>either(equalTo(3)).or(equalTo(4))));
assertThat(new Object(), not(sameInstance(new Object())));
}
  這里的 assertThat 用了兩種方法:一個是 JunitMatchers ,另一個是 hamcrest matchers 的 assertThat,不過后者提供的功能相當強大,前者的方法已經標為廢棄了。
  另外,官方也提及了其它第三方提供的 Matchers 實現:
  Excel spreadsheet matchers
  JSON matchers
  XML/XPath matchers
  所以再次我們只看后者,可以看出來的是其方法的后一個參數非常靈活,緊接著我們看看其怎么實現的?
  assertThat 方法實現
  public static <T> void assertThat(T actual, Matcher<? super T> matcher) {
  assertThat("", actual, matcher);
  }
  public static <T> void assertThat(String reason, T actual,
  Matcher<? super T> matcher) {
  MatcherAssert.assertThat(reason, actual, matcher);
  }
  再定位到 MatcherAssert 類的方法 assertThat:
  public static <T> void assertThat(String reason, T actual, Matcher<? super T> matcher) {
  if (!matcher.matches(actual)) {
  Description description = new StringDescription();
  description.appendText(reason)
  .appendText(" Expected: ")
  .appendDescriptionOf(matcher)
  .appendText("      but: ");
  matcher.describeMismatch(actual, description);
  throw new AssertionError(description.toString());
  }
  }
  可以看出真正地判斷方法是通過 Matcher 類的 matches 方法,若是不滿足的話,則返回 AssertionError。所以真正的核心是 Matcher,而關于它的實現都在 hamcrest-core-1.3 包中,看看其實現的類結構圖:

  看一下其的實現,可發現上文提到的 is , anyof 等等靜態方法都是返回一個相應的 Matcher,這樣通過一個簡單的抽象,在這里提供了極大的靈活性。若是感覺它提供的這些不滿足的話,也可自己進行來進行重寫,按自己的需求來定制實現。
  Rule 介紹
  同樣地,當我們越來越多需要進行單元測試時,需要使用 Rule 來幫忙了。其主要目的是針對一個測試類中的每個單元測試方法進行統一添加一些行為。代碼則使用 @Rule 注解的形式來添加至類的屬性上。
  在 Junit 框架中,其相對應的接口是 TestRule,而主要的實現有:
  ErrorCollector: 將大量的錯誤收集起來
  ExpectedException: 對拋出的錯誤做斷言
  ExternalResource: 可對測試方法的開始和結束添加回調
  TemporaryFolder: 用來創建文件,并在測試結束時自動刪除
  TestName: 用來獲取測試所執行的方法名稱
  TestWatcher: 可在測試方法的執行期間添加邏輯
  Timeout: 超過固定的時間讓測試結束
  Verifier: 當狀態不正確時,可讓測試結束
  它們的更多使用方法,可參照官網的 Rules 介紹。
  實現原理分析
  Junit4 中的測試代碼可被執行,是因為其真正的入口是名為 JUnitCore。它作為 Junit 的 Facade (門面)模式,來對外進行交互。另外,其有一個靜態的 main 方法:
  public static void main(String... args) {
  Result result = new JUnitCore().runMain(new RealSystem(), args);
  System.exit(result.wasSuccessful() ? 0 : 1);
  }
  所以,當我們執行單元測試的時候,其實也是運行了一個新的進程應用程序,其入口在這里。我們執行分析的時候,也從這里開始:

上一頁12下一頁
軟件測試工具 | 聯系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網站地圖
滬ICP備07036474 2003-2017 版權所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd
主站蜘蛛池模板: 波多野结衣在线视频观看 | 久久生活片 | 黄色一级免费片 | 国产乱插 | 国产成人综合亚洲 | 看全色黄一级毛片 | h视频在线观看视频观看 | 日韩在线成人 | 黄色三级一级片 | 男人把女人靠到爽痛视频 | 亚洲一卡二卡在线 | 男女性潮高片无遮挡禁18 | 日本高清免费h色视频在线观看 | 国产午夜视频在线观看第四页 | 亚洲欧美人成网站综合在线 | 天天看天天射天天碰 | 日本高清无卡码一区二区久久 | 亚洲无线码一区在线观看 | 国自产拍91大神精品 | 亚州黄色网址 | 国产丝袜大长腿精品丝袜美女 | 琪琪午夜免费影院在线观看 | 成年人午夜剧场 | 18黄网站 | japan高清日本乱xxxx | 日韩免费高清一级毛片在线 | 成人免费网站 | 色的视频在线观看免费播放 | 日韩伦理剧 | 一级黄色夫妻录像 | 成 人 黄 色 小 说网 | 欧美一区二区三区久久久 | 看黄网址在线观看 | 欧美精品人爱c欧美精品 | 26uuu精品一区二区 | 日本三级一区二区 | 国产精品午夜波多野结衣性色 | 欧美日本一二三区 | 欧美亚洲国产一区二区三区 | 成人欧美一区二区三区黑人免费 | 又黄又爽又猛大片录像 |