二、核心——斷言
斷言是編寫測試用例的核心實現方式,即期望值是多少,測試的結果是多少,以此來判斷測試是否通過。
1. 斷言核心方法
assertArrayEquals(expecteds, actuals) | 查看兩個數組是否相等。 |
assertEquals(expected, actual) | 查看兩個對象是否相等。類似於字符串比較使用的equals()方法 |
assertNotEquals(first, second) | 查看兩個對象是否不相等。 |
assertNull(object) | 查看對象是否爲空。 |
assertNotNull(object) | 查看對象是否不爲空。 |
assertSame(expected, actual) | 查看兩個對象的引用是否相等。類似於使用“==”比較兩個對象 |
assertNotSame(unexpected, actual) | 查看兩個對象的引用是否不相等。類似於使用“!=”比較兩個對象 |
assertTrue(condition) | 查看運行結果是否爲true。 |
assertFalse(condition) | 查看運行結果是否爲false。 |
assertThat(actual, matcher) | 查看實際值是否滿足指定的條件 |
fail() | 讓測試失敗 |
2. 示例
- package test;
- import static org.hamcrest.CoreMatchers.*;
- import static org.junit.Assert.*;
- import java.util.Arrays;
- import org.hamcrest.core.CombinableMatcher;
- import org.junit.Test;
- public class AssertTests {
- @Test
- public void testAssertArrayEquals() {
- byte[] expected = "trial".getBytes();
- byte[] actual = "trial".getBytes();
- org.junit.Assert.assertArrayEquals("failure - byte arrays not same", expected, actual);
- }
- @Test
- public void testAssertEquals() {
- org.junit.Assert.assertEquals("failure - strings not same", 5l, 5l);
- }
- @Test
- public void testAssertFalse() {
- org.junit.Assert.assertFalse("failure - should be false", false);
- }
- @Test
- public void testAssertNotNull() {
- org.junit.Assert.assertNotNull("should not be null", new Object());
- }
- @Test
- public void testAssertNotSame() {
- org.junit.Assert.assertNotSame("should not be same Object", new Object(), new Object());
- }
- @Test
- public void testAssertNull() {
- org.junit.Assert.assertNull("should be null", null);
- }
- @Test
- public void testAssertSame() {
- Integer aNumber = Integer.valueOf(768);
- org.junit.Assert.assertSame("should be same", aNumber, aNumber);
- }
- // JUnit Matchers assertThat
- @Test
- public void testAssertThatBothContainsString() {
- org.junit.Assert.assertThat("albumen", both(containsString("a")).and(containsString("b")));
- }
- @Test
- public void testAssertThathasItemsContainsString() {
- org.junit.Assert.assertThat(Arrays.asList("one", "two", "three"), hasItems("one", "three"));
- }
- @Test
- public void testAssertThatEveryItemContainsString() {
- org.junit.Assert.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())));
- }
- }
package test;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import java.util.Arrays;
import org.hamcrest.core.CombinableMatcher;
import org.junit.Test;
public class AssertTests {
@Test
public void testAssertArrayEquals() {
byte[] expected = "trial".getBytes();
byte[] actual = "trial".getBytes();
org.junit.Assert.assertArrayEquals("failure - byte arrays not same", expected, actual);
}
@Test
public void testAssertEquals() {
org.junit.Assert.assertEquals("failure - strings not same", 5l, 5l);
}
@Test
public void testAssertFalse() {
org.junit.Assert.assertFalse("failure - should be false", false);
}
@Test
public void testAssertNotNull() {
org.junit.Assert.assertNotNull("should not be null", new Object());
}
@Test
public void testAssertNotSame() {
org.junit.Assert.assertNotSame("should not be same Object", new Object(), new Object());
}
@Test
public void testAssertNull() {
org.junit.Assert.assertNull("should be null", null);
}
@Test
public void testAssertSame() {
Integer aNumber = Integer.valueOf(768);
org.junit.Assert.assertSame("should be same", aNumber, aNumber);
}
// JUnit Matchers assertThat
@Test
public void testAssertThatBothContainsString() {
org.junit.Assert.assertThat("albumen", both(containsString("a")).and(containsString("b")));
}
@Test
public void testAssertThathasItemsContainsString() {
org.junit.Assert.assertThat(Arrays.asList("one", "two", "three"), hasItems("one", "three"));
}
@Test
public void testAssertThatEveryItemContainsString() {
org.junit.Assert.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())));
}
}
三、核心——註解
1. 說明
@Before | 初始化方法 |
@After | 釋放資源 |
@Test | 測試方法,在這裏可以測試期望異常和超時時間 |
@Ignore | 忽略的測試方法 |
@BeforeClass | 針對所有測試,只執行一次,且必須爲static void |
@AfterClass | 針對所有測試,只執行一次,且必須爲static void |
@RunWith | 指定測試類使用某個運行器 |
@Parameters | 指定測試類的測試數據集合 |
@Rule | 允許靈活添加或重新定義測試類中的每個測試方法的行爲 |
@FixMethodOrder | 指定測試方法的執行順序 |
2. 執行順序
一個測試類單元測試的執行順序爲:
@BeforeClass –> @Before –> @Test –> @After –> @AfterClass
每一個測試方法的調用順序爲:
@Before –> @Test –> @After
3. 示例
- package test;
- import static org.junit.Assert.*;
- import org.junit.*;
- public class JDemoTest {
- @BeforeClass
- public static void setUpBeforeClass() throws Exception {
- System.out.println("in BeforeClass================");
- }
- @AfterClass
- public static void tearDownAfterClass() throws Exception {
- System.out.println("in AfterClass=================");
- }
- @Before
- public void before() {
- System.out.println("in Before");
- }
- @After
- public void after() {
- System.out.println("in After");
- }
- @Test(timeout = 10000)
- public void testadd() {
- JDemo a = new JDemo();
- assertEquals(6, a.add(3, 3));
- System.out.println("in Test ----Add");
- }
- @Test
- public void testdivision() {
- JDemo a = new JDemo();
- assertEquals(3, a.division(6, 2));
- System.out.println("in Test ----Division");
- }
- @Ignore
- @Test
- public void test_ignore() {
- JDemo a = new JDemo();
- assertEquals(6, a.add(1, 5));
- System.out.println("in test_ignore");
- }
- @Test
- public void teest_fail() {
- fail();
- }
- }
- class JDemo extends Thread {
- int result;
- public int add(int a, int b) {
- try {
- sleep(1000);
- result = a + b;
- } catch (InterruptedException e) {
- }
- return result;
- }
- public int division(int a, int b) {
- return result = a / b;
- }
- }
package test;
import static org.junit.Assert.*;
import org.junit.*;
public class JDemoTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
System.out.println("in BeforeClass================");
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
System.out.println("in AfterClass=================");
}
@Before
public void before() {
System.out.println("in Before");
}
@After
public void after() {
System.out.println("in After");
}
@Test(timeout = 10000)
public void testadd() {
JDemo a = new JDemo();
assertEquals(6, a.add(3, 3));
System.out.println("in Test ----Add");
}
@Test
public void testdivision() {
JDemo a = new JDemo();
assertEquals(3, a.division(6, 2));
System.out.println("in Test ----Division");
}
@Ignore
@Test
public void test_ignore() {
JDemo a = new JDemo();
assertEquals(6, a.add(1, 5));
System.out.println("in test_ignore");
}
@Test
public void teest_fail() {
fail();
}
}
class JDemo extends Thread {
int result;
public int add(int a, int b) {
try {
sleep(1000);
result = a + b;
} catch (InterruptedException e) {
}
return result;
}
public int division(int a, int b) {
return result = a / b;
}
}
執行結果:
- in BeforeClass================
- in Before
- in Test ----Add
- in After
- in Before
- in Test ----Division
- in After
- in AfterClass=================
in BeforeClass================
in Before
in Test ----Add
in After
in Before
in Test ----Division
in After
in AfterClass=================
圖中左上紅框中部分表示Junit運行結果,5個成功(1個忽略),1個錯誤,1個失敗。(注意錯誤和失敗不是一回事,錯誤說明代碼有錯誤,而失敗表示該測試方法測試失敗)
左下紅框中則表示出了各個測試方法的運行狀態,可以看到成功、錯誤、失敗、失敗各自的圖標是不一樣的,還可以看到運行時間。
右邊部分則是異常堆棧,可查看異常信息。
下篇中我們給出更多示例還繼續介紹Junit