课程表

JUnit课程

工具箱
速查手册

JUnit - 编写测试

当前位置:免费教程 » Java相关 » JUnit

在这里你将会看到一个应用 POJO 类,Business logic 类和在 test runner 中运行的 test 类的 JUnit 测试的例子。

C:\ > JUNIT_WORKSPACE 路径下创建一个名为 EmployeeDetails.java 的 POJO 类。

  1. public class EmployeeDetails {
  2. private String name;
  3. private double monthlySalary;
  4. private int age;
  5. /**
  6. * @return the name
  7. */
  8. public String getName() {
  9. return name;
  10. }
  11. /**
  12. * @param name the name to set
  13. */
  14. public void setName(String name) {
  15. this.name = name;
  16. }
  17. /**
  18. * @return the monthlySalary
  19. */
  20. public double getMonthlySalary() {
  21. return monthlySalary;
  22. }
  23. /**
  24. * @param monthlySalary the monthlySalary to set
  25. */
  26. public void setMonthlySalary(double monthlySalary) {
  27. this.monthlySalary = monthlySalary;
  28. }
  29. /**
  30. * @return the age
  31. */
  32. public int getAge() {
  33. return age;
  34. }
  35. /**
  36. * @param age the age to set
  37. */
  38. public void setAge(int age) {
  39. this.age = age;
  40. }
  41. }

EmployeeDetails 类被用于

  • 取得或者设置雇员的姓名的值
  • 取得或者设置雇员的每月薪水的值
  • 取得或者设置雇员的年龄的值

C:\ > JUNIT_WORKSPACE 路径下创建一个名为 EmpBusinessLogic.java 的 business logic 类

  1. public class EmpBusinessLogic {
  2. // Calculate the yearly salary of employee
  3. public double calculateYearlySalary(EmployeeDetails employeeDetails){
  4. double yearlySalary=0;
  5. yearlySalary = employeeDetails.getMonthlySalary() * 12;
  6. return yearlySalary;
  7. }
  8. // Calculate the appraisal amount of employee
  9. public double calculateAppraisal(EmployeeDetails employeeDetails){
  10. double appraisal=0;
  11. if(employeeDetails.getMonthlySalary() < 10000){
  12. appraisal = 500;
  13. }else{
  14. appraisal = 1000;
  15. }
  16. return appraisal;
  17. }
  18. }

EmpBusinessLogic 类被用来计算

  • 雇员每年的薪水
  • 雇员的评估金额

C:\ > JUNIT_WORKSPACE 路径下创建一个名为 TestEmployeeDetails.java 的准备被测试的测试案例类

  1. import org.junit.Test;
  2. import static org.junit.Assert.assertEquals;
  3. public class TestEmployeeDetails {
  4. EmpBusinessLogic empBusinessLogic =new EmpBusinessLogic();
  5. EmployeeDetails employee = new EmployeeDetails();
  6. //test to check appraisal
  7. @Test
  8. public void testCalculateAppriasal() {
  9. employee.setName("Rajeev");
  10. employee.setAge(25);
  11. employee.setMonthlySalary(8000);
  12. double appraisal= empBusinessLogic.calculateAppraisal(employee);
  13. assertEquals(500, appraisal, 0.0);
  14. }
  15. // test to check yearly salary
  16. @Test
  17. public void testCalculateYearlySalary() {
  18. employee.setName("Rajeev");
  19. employee.setAge(25);
  20. employee.setMonthlySalary(8000);
  21. double salary= empBusinessLogic.calculateYearlySalary(employee);
  22. assertEquals(96000, salary, 0.0);
  23. }
  24. }

TestEmployeeDetails 是用来测试 EmpBusinessLogic 类的方法的,它

  • 测试雇员的每年的薪水
  • 测试雇员的评估金额

现在让我们在 C:\ > JUNIT_WORKSPACE 路径下创建一个名为 TestRunner.java 的类来执行测试案例类

  1. import org.junit.runner.JUnitCore;
  2. import org.junit.runner.Result;
  3. import org.junit.runner.notification.Failure;
  4. public class TestRunner {
  5. public static void main(String[] args) {
  6. Result result = JUnitCore.runClasses(TestEmployeeDetails.class);
  7. for (Failure failure : result.getFailures()) {
  8. System.out.println(failure.toString());
  9. }
  10. System.out.println(result.wasSuccessful());
  11. }
  12. }

用javac编译 Test case 和 Test Runner 类

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

现在运行将会运行 Test Case 类中定义和提供的测试案例的 Test Runner

  1. C:\JUNIT_WORKSPACE>java TestRunner

检查运行结果

  1. true
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号