JUnit - 编写测试

JUnit - 编写测试 首页 / JUnit入门教程 / 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;
   }
}

在C:\> JUNIT_WORKSPACE中创建一个名为EmpBusinessLogic.java的文件,其中包含业务逻辑。

public class EmpBusinessLogic {
   //计算员工的年薪
   public double calculateYearlySalary(EmployeeDetails employeeDetails) {
      double yearlySalary = 0;
      yearlySalary = employeeDetails.getMonthlySalary() * 12;
      return yearlySalary;
   }
	
   //计算员工的评估金额
   public double calculateAppraisal(EmployeeDetails employeeDetails) {
      double appraisal = 0;
		
      if(employeeDetails.getMonthlySalary() < 10000){
         appraisal = 500;
      }else{
         appraisal = 1000;
      }
		
      return appraisal;
   }
}

在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
   public void testCalculateAppriasal() {
      employee.setName("Rajeev");
      employee.setAge(25);
      employee.setMonthlySalary(8000);
		
      double appraisal = empBusinessLogic.calculateAppraisal(employee);
      assertEquals(500, appraisal, 0.0);
   }

   //测试检查年薪
   @Test
   public void testCalculateYearlySalary() {
      employee.setName("Rajeev");
      employee.setAge(25);
      employee.setMonthlySalary(8000);
		
      double salary = empBusinessLogic.calculateYearlySalary(employee);
      assertEquals(96000, salary, 0.0);
   }
}

接下来,在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编译测试用例和Test Runner类。

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

现在运行Test Runner,它将运行在提供的Test Case类中定义的测试用例。

C:\JUNIT_WORKSPACE>java TestRunner

验证输出。

链接:https://www.learnfk.comhttps://www.learnfk.com/junit/junit-writing-tests.html

来源:LearnFk无涯教程网

true

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

左耳听风 -〔陈皓〕

Python核心技术与实战 -〔景霄〕

黄勇的OKR实战笔记 -〔黄勇〕

从0打造音视频直播系统 -〔李超〕

职场求生攻略 -〔臧萌〕

大厂晋升指南 -〔李运华〕

HarmonyOS快速入门与实战 -〔QCon+案例研习社〕

反爬虫兵法演绎20讲 -〔DS Hunter〕

徐昊 · TDD项目实战70讲 -〔徐昊〕

好记忆不如烂笔头。留下您的足迹吧 :)