JUnit - 編寫測試



這裡,我們將看到一個使用 POJO 類、業務邏輯類和測試類(由測試執行器執行)的 JUnit 測試完整示例。

在 C:\>JUNIT_WORKSPACE 中建立 EmployeeDetails.java,這是一個 POJO 類。

public class EmployeeDetails {

   private String name;
   private double monthlySalary;
   private int age;
   
   /**
   * @return the name
   */
	
   public String getName() {
      return name;
   }
	
   /**
   * @param name the name to set
   */
	
   public void setName(String name) {
      this.name = name;
   }
	
   /**
   * @return the monthlySalary
   */
	
   public double getMonthlySalary() {
      return monthlySalary;
   }
	
   /**
   * @param monthlySalary the monthlySalary to set
   */
	
   public void setMonthlySalary(double monthlySalary) {
      this.monthlySalary = monthlySalary;
   }
	
   /**
   * @return the age
   */
   public int getAge() {
      return age;
   }
	
   /**
   * @param age the age to set
   */
   public void setAge(int age) {
      this.age = age;
   }
}

EmployeeDetails 類用於:−

  • 獲取/設定員工姓名的值。
  • 獲取/設定員工月薪的值。
  • 獲取/設定員工年齡的值。

在 C:\>JUNIT_WORKSPACE 中建立一個名為 EmpBusinessLogic.java 的檔案,其中包含業務邏輯。

public class EmpBusinessLogic {
   // Calculate the yearly salary of employee
   public double calculateYearlySalary(EmployeeDetails employeeDetails) {
      double yearlySalary = 0;
      yearlySalary = employeeDetails.getMonthlySalary() * 12;
      return yearlySalary;
   }
	
   // Calculate the appraisal amount of employee
   public double calculateAppraisal(EmployeeDetails employeeDetails) {
      double appraisal = 0;
		
      if(employeeDetails.getMonthlySalary() < 10000){
         appraisal = 500;
      }else{
         appraisal = 1000;
      }
		
      return appraisal;
   }
}

EmpBusinessLogic 類用於計算:−

  • 員工的年薪。
  • 員工的評估金額。

在 C:\>JUNIT_WORKSPACE 中建立一個名為 TestEmployeeDetails.java 的檔案,其中包含要測試的測試用例。

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

public class TestEmployeeDetails {
   EmpBusinessLogic empBusinessLogic = new EmpBusinessLogic();
   EmployeeDetails employee = new EmployeeDetails();

   //test to check appraisal
   @Test
   public void testCalculateAppriasal() {
      employee.setName("Rajeev");
      employee.setAge(25);
      employee.setMonthlySalary(8000);
		
      double appraisal = empBusinessLogic.calculateAppraisal(employee);
      assertEquals(500, appraisal, 0.0);
   }

   // test to check yearly salary
   @Test
   public void testCalculateYearlySalary() {
      employee.setName("Rajeev");
      employee.setAge(25);
      employee.setMonthlySalary(8000);
		
      double salary = empBusinessLogic.calculateYearlySalary(employee);
      assertEquals(96000, salary, 0.0);
   }
}

TestEmployeeDetails 類用於測試 EmpBusinessLogic 類的各個方法。它

  • 測試員工的年薪。
  • 測試員工的評估金額。

接下來,在 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(TestEmployeeDetails.class);
		
      for (Failure failure : result.getFailures()) {
         System.out.println(failure.toString());
      }
		
      System.out.println(result.wasSuccessful());
   }
} 

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

C:\JUNIT_WORKSPACE>javac EmployeeDetails.java 
EmpBusinessLogic.java TestEmployeeDetails.java TestRunner.java

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

C:\JUNIT_WORKSPACE>java TestRunner

驗證輸出。

true
廣告