扫码一下
查看教程更方便
在这里你将会看到一个应用 pojo 类,business logic
类和在 test runner 中运行的 test 类的 junit 测试的例子。
在 c:\ > junit_workspace 路径下创建一个名为 employeedetails.java 的 pojo 类。
employeedetails.java
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 的 business logic
类
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 的准备被测试的测试案例类
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 的类来执行测试案例类
testrunner.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 case 和 test runner 类
c:\junit_workspace>javac employeedetails.java
empbusinesslogic.java testemployeedetails.java testrunner.java
现在运行将会运行 test case 类中定义和提供的测试案例的 test runner
c:\junit_workspace>java testrunner
检查运行结果
true