Junit-writing-tests

提供:Dev Guides
移動先:案内検索

JUnit-テストの作成

ここでは、テストランナーによって実行される、POJOクラス、ビジネスロジッククラス、およびテストクラスを使用したJUnitテストの完全な例が1つ表示されます。

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* クラスは計算に使用されます-
  • 従業員の年sal。
  • 従業員の評価額。

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* クラスのメソッドのテストに使用されます。 It
  • 従業員の年salをテストします。
  • 従業員の評価額をテストします。

次に、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