JUnit - 斷言的使用



斷言

所有斷言都在 Assert 類中。

public class Assert extends java.lang.Object

此類提供了一組斷言方法,可用於編寫測試。僅記錄失敗的斷言。Assert 類的一些重要方法如下:

序號 方法和描述
1

void assertEquals(boolean expected, boolean actual)

檢查兩個基本型別/物件是否相等。

2

void assertTrue(boolean condition)

檢查條件是否為真。

3

void assertFalse(boolean condition)

檢查條件是否為假。

4

void assertNotNull(Object object)

檢查物件是否不為空。

5

void assertNull(Object object)

檢查物件是否為空。

6

void assertSame(object1, object2)

assertSame() 方法測試兩個物件引用是否指向同一個物件。

7

void assertNotSame(object1, object2)

assertNotSame() 方法測試兩個物件引用是否不指向同一個物件。

8

void assertArrayEquals(expectedArray, resultArray);

assertArrayEquals() 方法將測試兩個陣列是否彼此相等。

讓我們在一個示例中使用上述某些方法。在 C:\>JUNIT_WORKSPACE 中建立一個名為 **TestAssertions.java** 的 Java 類檔案。

import org.junit.Test;
import static org.junit.Assert.*;

public class TestAssertions {

   @Test
   public void testAssertions() {
      //test data
      String str1 = new String ("abc");
      String str2 = new String ("abc");
      String str3 = null;
      String str4 = "abc";
      String str5 = "abc";
		
      int val1 = 5;
      int val2 = 6;

      String[] expectedArray = {"one", "two", "three"};
      String[] resultArray =  {"one", "two", "three"};

      //Check that two objects are equal
      assertEquals(str1, str2);

      //Check that a condition is true
      assertTrue (val1 < val2);

      //Check that a condition is false
      assertFalse(val1 > val2);

      //Check that an object isn't null
      assertNotNull(str1);

      //Check that an object is null
      assertNull(str3);

      //Check if two object references point to the same object
      assertSame(str4,str5);

      //Check if two object references not point to the same object
      assertNotSame(str1,str3);

      //Check whether two arrays are equal to each other.
      assertArrayEquals(expectedArray, resultArray);
   }
}

接下來,在 C:\>JUNIT_WORKSPACE 中建立一個名為 **TestRunner.java** 的 Java 類檔案來執行測試用例。

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

public class TestRunner2 {
   public static void main(String[] args) {
      Result result = JUnitCore.runClasses(TestAssertions.class);
		
      for (Failure failure : result.getFailures()) {
         System.out.println(failure.toString());
      }
		
      System.out.println(result.wasSuccessful());
   }
} 

使用 javac 編譯測試用例和測試執行器類。

C:\JUNIT_WORKSPACE>javac TestAssertions.java TestRunner.java

現在執行測試執行器,它將執行在提供的測試用例類中定義的測試用例。

C:\JUNIT_WORKSPACE>java TestRunner

驗證輸出。

true

註解

註解類似於元標籤,可以新增到程式碼中,並將其應用於方法或類。JUnit 中的這些註解提供了關於測試方法的以下資訊:

  • 哪些方法將在測試方法之前和之後執行。
  • 哪些方法在所有方法之前和之後執行,以及。
  • 執行過程中將忽略哪些方法或類。

下表列出了 JUnit 中的註解及其含義:

序號 註解和描述
1

@Test

Test 註解告訴 JUnit,與其關聯的 public void 方法可以作為測試用例執行。

2

@Before

幾個測試需要在執行之前建立類似的物件。用 @Before 註解 public void 方法會導致該方法在每個 Test 方法之前執行。

3

@After

如果在 Before 方法中分配外部資源,則需要在測試執行後釋放它們。用 @After 註解 public void 方法會導致該方法在 Test 方法之後執行。

4

@BeforeClass

用 @BeforeClass 註解 public static void 方法會導致它在類中的任何測試方法之前執行一次。

5

@AfterClass

這將在所有測試完成後執行該方法。這可用於執行清理活動。

6

@Ignore

Ignore 註解用於忽略測試,並且該測試將不會執行。

在 C:\>JUNIT_WORKSPACE 中建立一個名為 **JunitAnnotation.java** 的 Java 類檔案來測試註解。

import org.junit.After;
import org.junit.AfterClass;

import org.junit.Before;
import org.junit.BeforeClass;

import org.junit.Ignore;
import org.junit.Test;

public class JunitAnnotation {
	
   //execute before class
   @BeforeClass
   public static void beforeClass() {
      System.out.println("in before class");
   }

   //execute after class
   @AfterClass
   public static void  afterClass() {
      System.out.println("in after class");
   }

   //execute before test
   @Before
   public void before() {
      System.out.println("in before");
   }
	
   //execute after test
   @After
   public void after() {
      System.out.println("in after");
   }
	
   //test case
   @Test
   public void test() {
      System.out.println("in test");
   }
	
   //test case ignore and will not execute
   @Ignore
   public void ignoreTest() {
      System.out.println("in ignore test");
   }
}

接下來,在 C:\>JUNIT_WORKSPACE 中建立一個名為 **TestRunner.java** 的 Java 類檔案來執行註解。

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

public class TestRunner {
   public static void main(String[] args) {
      Result result = JUnitCore.runClasses(JunitAnnotation.class);
		
      for (Failure failure : result.getFailures()) {
         System.out.println(failure.toString());
      }
		
      System.out.println(result.wasSuccessful());
   }
} 

使用 javac 編譯測試用例和測試執行器類。

C:\JUNIT_WORKSPACE>javac JunitAnnotation.java TestRunner.java

現在執行測試執行器,它將執行在提供的測試用例類中定義的測試用例。

C:\JUNIT_WORKSPACE>java TestRunner

驗證輸出。

in before class
in before
in test
in after
in after class
true
廣告