前言
前幾篇文章,讓我們對單元測試有了基本認識,從前面的練習可以稍微知道單元測試需要做哪些事情,可以透過Annotation幫我們在測試上提供程序上的輔助。目前我們都可以順利地對程式進行測試,接下來稍微認識一下Assert類別的用法,看看這個它能提供些什麼?Assert類別
Assert類別提供許多靜態(static)方法,讓我們可以用來聲明某個特定的假設應該成立。為何要用這個類別?我們也可以在測試程式中加入if-else的判斷,就像先前提到的,我可以像手動測試一樣,寫些判斷,不是也能達到同樣目的?還記得單元測試應該就是要簡單、易讀?加上單元測試也不該出現判斷語句,因為當你出現判斷邏輯時,也表示你應該對它進行測試,這樣不就沒完沒了?所以我們應該使用Assert類別,並且當你測試失敗時,它會對你提出警告,藉由Assert類別在斷言失敗時給予你什麼訊息,好讓你對程式碼進行修正。- assertEquals
- assertEquals(String message, Object expected, Object actual)
- assertEquals(Object expected, Object actual)
- assertEquals(String message, String expected, String actual)
- assertEquals(String expected, String actual)
- assertEquals(String message, double expected, double actual, double delta)
- assertEquals(double expected, double actual, double delta)
- assertEquals(String message, float expected, float actual, float delta)
- assertEquals(float expected, float actual, float delta)
- assertEquals(String message, long expected, long actual)
- assertEquals(long expected, long actual)
- assertEquals(String message, boolean expected, boolean actual)
- assertEquals(boolean expected, boolean actual)
- assertEquals(String message, byte expected, byte actual)
- assertEquals(byte expected, byte actual)
- assertEquals(String message, char expected, char actual)
- assertEquals(char expected, char actual)
- assertEquals(String message, short expected, short actual)
- assertEquals(short expected, short actual)
- assertEquals(String message, int expected, int actual)
- assertEquals(int expected, int actual)
@Test
public void testAssertEquals() throws Exception {
String s1 = new String("Test");
String s2 = new String("Test");
assertEquals(s1, s2);
}
@Test
public void testAssertEqualsWithMessage() throws Exception {
String s1 = new String("Test1");
String s2 = new String("Test2");
assertEquals("測試結果", s1, s2);
}
junit.framework.ComparisonFailure: 測試結果
Expected :Test1
Actual :Test2
- assertSame
- assertSame(String message, Object expected, Object actual)
- assertSame(Object expected, Object actual)
- assertNotSame
- assertNotSame(String message, Object expected, Object actual)
- assertNotSame(Object expected, Object actual)
public class AssertSameTest {
@Test
public void testAssertSame1() throws Exception {
// 兩個物件各分配了一個記憶體空間,所以不一樣。
String s1 = new String("test1");
String s2 = new String("test1");
assertNotSame(s1, s2);
}
@Test
public void testAssertSame2() throws Exception {
// s2參照s1同一個記憶體空間,所以一樣。
String s1 = new String("test1");
String s2 = s1;
assertSame(s1, s2);
}
@Test
public void testAssertSame3() throws Exception {
// s1分配了一個記憶體空間,而s2使用字串池(String pool),所以不一樣。
String s1 = new String("test1");
String s2 = "test1";
assertNotSame(s1, s2);
}
@Test
public void testAssertSame4() throws Exception {
// s1與s2都使用字串池,為了節省記憶體空間,只要字串池內的值相同,都是指向同一個記憶體空間,所以一樣。
String s1 = "test1";
String s2 = "test1";
assertSame(s1, s2);
}
}
- assertTrue(boolean condition)
- assertFalse
- assertFalse(String message, boolean condition)
- assertFalse(boolean condition)
public class AssertTrueTest {
@Test
public void testAssertTrue() throws Exception {
boolean actualTrue = true;
assertTrue(actualTrue);
}
@Test
public void testAssertFalse() throws Exception {
boolean actualFalse = false;
assertFalse(actualFalse);
}
@Test
public void testAssertFalseWithMessage() throws Exception {
boolean actualTrue = true;
assertFalse("測試失敗", actualTrue);
}
}
junit.framework.AssertionFailedError: 測試失敗
- fail
- fail(String message)
- fail()
@Test
public void testFailWithMessage() throws Exception {
fail("測試失敗");
}
junit.framework.AssertionFailedError: 測試失敗
@Test
public void testFail() throws Exception {
fail();
}
junit.framework.AssertionFailedError
- assertNotNull
- assertNotNull(Object object)
- assertNotNull(String message, Object object)
- assertNull
- assertNull(Object object)
- assertNull(String message, Object object)
@Test
public void testNull() throws Exception {
Object obj = null;
assertNull(obj);
}
@Test
public void testNotNullWithMessage() throws Exception {
Object obj = null;
assertNotNull("測試失敗", obj);
}
junit.framework.AssertionFailedError: 測試失敗
- assertArrayEquals
與assertEquals類似,也提供測試失敗時的訊息顯示,但用來判斷陣列裡面的內容是否相同,可省去單用assertEquals的麻煩,程式碼如下。
@Test
public void testAssertArrayEqualsTest() throws Exception {
String[] array1 = new String[]{"John", "Xavier", "Same", "Justin"};
String[] array2 = new String[]{"John", "Xavier", "Same", "Justin"};
assertArrayEquals(array1, array2);
}
沒有留言:
張貼留言