Junit使用及其原理分析 (上)

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等。

  代碼示例

@Testpublic void testAssertArrayEquals() {byte[] expected = "trial".getBytes();byte[] actual = "trial".getBytes();assertArrayEquals("failure - byte arrays not same", expected, actual);}@Testpublic void testAssertEquals() {assertEquals("failure - strings are not equal", "text", "text");}@Testpublic void testAssertFalse() {assertFalse("failure - should be false", false);}@Testpublic void testAssertNotNull() {assertNotNull("should not be null", new Object());}@Testpublic void testAssertNotSame() {assertNotSame("should not be same Object", new Object(), new Object());}@Testpublic void testAssertNull() {assertNull("should be null", null);}@Testpublic void testAssertSame() {Integer aNumber = Integer.valueOf(768);assertSame("should be same", aNumber, aNumber);}@Testpublic void testAssertTrue() {assertTrue("failure - should be true", true);}

以上代碼來自官方介紹的 Demo , 列舉的是常用而又基礎的操作,但遇到複雜的集合判斷操作,就力不從心了,不過 Junit 提供了另一更為強大的 assertThat 方法,首先來看看它的使用:

//JUnitMatchersassertThat

@TestpublicvoidtestAssertThatBothContainsString(){assertThat("albumen",both(containsString("a")).and(containsString("b")));}@TestpublicvoidtestAssertThatHasItems(){assertThat(Arrays.asList("one","two","three"),hasItems("one","three"));}@TestpublicvoidtestAssertThatEveryItemContainsString(){assertThat(Arrays.asList(newString[]{"fun","ban","net"}),everyItem(containsString("n")));}//CoreHamcrestMatcherswithassertThat@TestpublicvoidtestAssertThatHamcrestCoreMatchers(){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(newObject(),not(sameInstance(newObject())));}  

  這裡的 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 包中,看看其實現的類結構圖:


推薦閱讀:

機械設計新人獨立思考能力養成
小型五金批量處理自動噴砂機
PLC系統一些實用的檢測和維護知識
無人值守時代,運維如何保障發布質量?

TAG:性能測試 | jmeter | 自動化 |