JUnit - 編寫(xiě)測(cè)試

2022-02-23 18:03 更新

在這里你將會(huì)看到一個(gè)應(yīng)用 POJO 類,Business logic 類和在 test runner 中運(yùn)行的 test 類的 JUnit 測(cè)試的例子。

C:\ > JUNIT_WORKSPACE 路徑下創(chuàng)建一個(gè)名為 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 類被用于

  • 取得或者設(shè)置雇員的姓名的值
  • 取得或者設(shè)置雇員的每月薪水的值
  • 取得或者設(shè)置雇員的年齡的值

C:\ > JUNIT_WORKSPACE 路徑下創(chuàng)建一個(gè)名為 EmpBusinessLogic.java 的 business logic 類

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 類被用來(lái)計(jì)算

  • 雇員每年的薪水
  • 雇員的評(píng)估金額

C:\ > JUNIT_WORKSPACE 路徑下創(chuàng)建一個(gè)名為 TestEmployeeDetails.java 的準(zhǔn)備被測(cè)試的測(cè)試案例類

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 是用來(lái)測(cè)試 EmpBusinessLogic 類的方法的,它

  • 測(cè)試雇員的每年的薪水
  • 測(cè)試雇員的評(píng)估金額

現(xiàn)在讓我們?cè)?C:\ > JUNIT_WORKSPACE 路徑下創(chuàng)建一個(gè)名為 TestRunner.java 的類來(lái)執(zhí)行測(cè)試案例類

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編譯 Test case 和 Test Runner 類

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

現(xiàn)在運(yùn)行將會(huì)運(yùn)行 Test Case 類中定義和提供的測(cè)試案例的 Test Runner

C:\JUNIT_WORKSPACE>java TestRunner

檢查運(yùn)行結(jié)果

true


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)