鍍金池/ 教程/ Java/ JUnit - 編寫測試
JUnit - 參數(shù)化測試
JUnit - 執(zhí)行過程
JUnit - 執(zhí)行測試
JUnit - 概述
JUnit - 測試框架
JUnit - 套件測試
Junit - 環(huán)境設置
JUnit - 忽略測試
JUnit - 使用斷言
JUnit - Eclipse 插件
JUnit - 時間測試
JUnit - 編寫測試
JUnit - 框架擴展
JUnit - API
JUnit - 異常測試
JUnit - ANT 插件
JUnit - 基本用法

JUnit - 編寫測試

在這里你將會看到一個應用 POJO 類,Business logic 類和在 test runner 中運行的 test 類的 JUnit 測試的例子。

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

  • 雇員每年的薪水
  • 雇員的評估金額

C:\ > JUNIT_WORKSPACE 路徑下創(chuàng)建一個名為 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 類的方法的,它

  • 測試雇員的每年的薪水
  • 測試雇員的評估金額

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

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)在運行將會運行 Test Case 類中定義和提供的測試案例的 Test Runner

C:\JUNIT_WORKSPACE>java TestRunner

檢查運行結果

true