- JUnit 教程
- JUnit - 主页
- JUnit - 概述
- JUnit - 环境设置
- JUnit - 测试框架
- JUnit - 基本用法
- JUnit - API
- JUnit - 编写测试
- JUnit - 使用断言
- JUnit - 执行过程
- JUnit - 执行测试
- JUnit - 套件测试
- JUnit - 忽略测试
- JUnit - 时间测试
- JUnit - 异常测试
- JUnit - 参数化测试
- JUnit - 与 Ant 集成
- JUnit - 与 Eclipse 集成
- JUnit - 扩展
- JUnit 实用资源
- JUnit - 问题与解答
- 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;
}
}
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
广告