Java: Thủ tục thực thi


Khóa học qua video:
Lập trình Python All Lập trình C# All SQL Server All Lập trình C All Java PHP HTML5-CSS3-JavaScript
Đăng ký Hội viên
Tất cả các video dành cho hội viên

Bài viết này giải thích thủ tục thực thi của các phương thức trong JUnit, nó xác định thứ tự của các phương thức được gọi. Thảo luận bên dưới là quy trình thực thi của các phương thức API test JUnit với ví dụ kèm theo.

Tạo file java có tên ExecutionProcedureJunit.java trong C:\>JUNIT_WORKSPACE để test annotation.

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 ExecutionProcedureJunit {
	
   //execute only once, in the starting 
   @BeforeClass
   public static void beforeClass() {
      System.out.println("in before class");
   }

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

   //execute for each test, before executing test
   @Before
   public void before() {
      System.out.println("in before");
   }
	
   //execute for each test, after executing test
   @After
   public void after() {
      System.out.println("in after");
   }
	
   //test case 1
   @Test
   public void testCase1() {
      System.out.println("in test case 1");
   }

   //test case 2
   @Test
   public void testCase2() {
      System.out.println("in test case 2");
   }
}

Tiếp theo, tạo một file java có tên TestRunner.java trong C:\>JUNIT_WORKSPACE để thực thi các annotation (chú thích).

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(ExecutionProcedureJunit.class);

      for (Failure failure : result.getFailures()) {
         System.out.println(failure.toString());
      }
		
      System.out.println(result.wasSuccessful());
   }
} 

Biên dịch các lớp Test case và Test Runner bằng javac.

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

Bây giờ hãy chạy Test Runner, nó sẽ chạy test case được xác định trong lớp Test Case đã cung cấp.

C:\JUNIT_WORKSPACE>java TestRunner

Xác minh kết quả đầu ra.

in before class
in before
in test case 1
in after
in before
in test case 2
in after
in after class

Quy trình thực hiện để được kết quả đầu ra như ở trên:

  • Trước hết, phương thức beforeClass() chỉ thực thi một lần.
  • Phương thức afterClass() chỉ thực thi một lần.
  • Phương thức before() thực thi cho mỗi test case, nhưng trước khi thực thi test case.
  • Phương thức after() thực thi cho mỗi test case, nhưng sau khi thực thi test case.
  • Ở giữa before() và after(), thì từng test case sẽ được thực thi.
» Tiếp: Thực thi các test
« Trước: Sử dụng Assertion
Khóa học qua video:
Lập trình Python All Lập trình C# All SQL Server All Lập trình C All Java PHP HTML5-CSS3-JavaScript
Đăng ký Hội viên
Tất cả các video dành cho hội viên
Copied !!!