0


【JUnit技术专题】「入门到精通系列」手把手+零基础带你玩转单元测试,让你的代码更加“强壮”(实战开发篇)

手把手+零基础带你玩转单元测试,让你的代码更加“强壮”(实战开发篇)

实战开发篇

本节内容主要介绍JUnit单元测试功能框架,并以实战演练的形式进行讲解。本系列教程主要针对代码编程方式和模型,重点讲解实战代码开发。通过本系列教程的学习,您将能够深入了解JUnit单元测试框架的使用和原理,并掌握如何在实际项目中运用JUnit进行单元测试。

创建Pojo模型

以下是一个使用JUnit对业务逻辑类和测试运行器中的测试类进行测试的示例。首先,我们需要创建一个名:EmployeeDetails.java的POJO类。

EmployeeDetails 类被用于

  • 取得或者设置雇员的姓名的值。
  • 取得或者设置雇员的每月薪水的值。
  • 取得或者设置雇员的年龄的值。
publicclassEmployeeDetails{privateString name;privatedouble monthlySalary;privateint age;/**
    * @return the name
    */publicStringgetName(){return name;}/**
    * @param name the name to set
    */publicvoidsetName(String name){this.name = name;}/**
    * @return the monthlySalary
    */publicdoublegetMonthlySalary(){return monthlySalary;}/**
    * @param monthlySalary the monthlySalary to set
    */publicvoidsetMonthlySalary(double monthlySalary){this.monthlySalary = monthlySalary;}/**
    * @return the age
    */publicintgetAge(){return age;}/**
    * @param age the age to set
    */publicvoidsetAge(int age){this.age = age;}}

EmpBusinessLogic 类被用来计算:

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

创建一个名为 EmpBusinessLogic.java 的 business logic 类:

publicclassEmpBusinessLogic{// Calculate the yearly salary of employeepublicdoublecalculateYearlySalary(EmployeeDetails employeeDetails){double yearlySalary=0;
        yearlySalary = employeeDetails.getMonthlySalary()*12;return yearlySalary;}// Calculate the appraisal amount of employeepublicdoublecalculateAppraisal(EmployeeDetails employeeDetails){double appraisal=0;if(employeeDetails.getMonthlySalary()<10000){
            appraisal =500;}else{
            appraisal =1000;}return appraisal;}}}

创建一个名为 TestEmployeeDetails.java 的准备被测试的测试案例类

importorg.junit.Test;importstaticorg.junit.Assert.assertEquals;publicclassTestEmployeeDetails{EmpBusinessLogic empBusinessLogic =newEmpBusinessLogic();EmployeeDetails employee =newEmployeeDetails();//test to check appraisal@TestpublicvoidtestCalculateAppriasal(){
      employee.setName("Rajeev");
      employee.setAge(25);
      employee.setMonthlySalary(8000);double appraisal= empBusinessLogic.calculateAppraisal(employee);assertEquals(500, appraisal,0.0);}// test to check yearly salary@TestpublicvoidtestCalculateYearlySalary(){
       employee.setName("Rajeev");
       employee.setAge(25);
       employee.setMonthlySalary(8000);double salary= empBusinessLogic.calculateYearlySalary(employee);assertEquals(96000, salary,0.0);}}

创建一个名为 TestRunner.java 的类来执行测试案例类:

importorg.junit.runner.JUnitCore;importorg.junit.runner.Result;importorg.junit.runner.notification.Failure;publicclassTestRunner{publicstaticvoidmain(String[] args){Result result =JUnitCore.runClasses(TestEmployeeDetails.class);for(Failure failure : result.getFailures()){System.out.println(failure.toString());}System.out.println(result.wasSuccessful());}}

断言操作

所有的断言都包含在 Assert 类中

publicclassAssertextendsjava.lang.Object

该类提供了许多有用的断言方法,可用于编写测试用例。仅记录失败的断言。以下是Assert类中一些有用的方法的示例:
序号方法和描述1void assertEquals(boolean expected, boolean actual) 检查两个变量或者等式是否成立2void assertTrue(boolean expected, boolean actual) 检查条件为真)3void assertFalse(boolean condition) 检查条件为假4void assertNotNull(Object object) 检查对象不为空5void assertNull(Object object) 检查对象为空6void assertSame(boolean condition) assertSame() 方法检查两个相关对象是否指向同一个对象7void assertNotSame(boolean condition) assertNotSame() 方法检查两个相关对象是否不指向同一个对象8void assertArrayEquals(expectedArray, resultArray) assertArrayEquals() 方法检查两个数组是否相等
下面我们在例子中试验一下上面提到的各种方法。创建一个文件名为 TestAssertions.java 的类

importorg.junit.Test;importstaticorg.junit.Assert.*;publicclassTestAssertions{@TestpublicvoidtestAssertions(){//test dataString str1 =newString("abc");String str2 =newString("abc");String str3 =null;String str4 ="abc";String str5 ="abc";int val1 =5;int val2 =6;String[] expectedArray ={"one","two","three"};String[] resultArray ={"one","two","three"};//Check that two objects are equalassertEquals(str1, str2);//Check that a condition is true
        assertTrue (val1 < val2);//Check that a condition is falseassertFalse(val1 > val2);//Check that an object isn't nullassertNotNull(str1);//Check that an object is nullassertNull(str3);//Check if two object references point to the same objectassertSame(str4,str5);//Check if two object references not point to the same objectassertNotSame(str1,str3);//Check whether two arrays are equal to each other.assertArrayEquals(expectedArray, resultArray);}}

接下来,创建一个文件名为 TestRunner.java 的类来执行测试用例:

importorg.junit.runner.JUnitCore;importorg.junit.runner.Result;importorg.junit.runner.notification.Failure;publicclassTestRunner2{publicstaticvoidmain(String[] args){Result result =JUnitCore.runClasses(TestAssertions.class);for(Failure failure : result.getFailures()){System.out.println(failure.toString());}System.out.println(result.wasSuccessful());}}

注解Annotation

注解就好像你可以在你的代码中添加并且在方法或者类中应用的元标签。JUnit 中的这些注释为我们提供了测试
方法的相关信息,哪些方法将会在测试方法前后应用,哪些方法将会在所有方法前后应用,哪些方法将会在执行
中被忽略。

JUnit 中的注解的列表以及他们的含义:
序号注释和描述1@Test 这个注释说明依附在 JUnit 的 public void 方法可以作为一个测试案例。2@Before 有些测试在运行前需要创造几个相似的对象。在 public void 方法加该注释是因为该方法需要在 test 方法前运行。3@After 如果你将外部资源在 Before 方法中分配,那么你需要在测试运行后释放他们。在 public void 方法加该注释是因为该方法需要在 test 方法后运行。4@BeforeClass 在 public void 方法加该注释是因为该方法需要在类中所有方法前运行。5@AfterClass它将会使方法在所有测试结束后执行。这个可以用来进行清理活动。6@Ignore这个注释是用来忽略有关不需要执行的测试的。
下创建一个文件名为 JunitAnnotation.java 的类来测试注释

importorg.junit.After;importorg.junit.AfterClass;importorg.junit.Before;importorg.junit.BeforeClass;importorg.junit.Ignore;importorg.junit.Test;publicclassJunitAnnotation{//execute before class@BeforeClasspublicstaticvoidbeforeClass(){System.out.println("in before class");}//execute after class@AfterClasspublicstaticvoidafterClass(){System.out.println("in after class");}//execute before test@Beforepublicvoidbefore(){System.out.println("in before");}//execute after test@Afterpublicvoidafter(){System.out.println("in after");}//test case@Testpublicvoidtest(){System.out.println("in test");}//test case ignore and will not execute@IgnorepublicvoidignoreTest(){System.out.println("in ignore test");}}

创建一个文件名为 TestRunner.java 的类来执行注解

importorg.junit.runner.JUnitCore;importorg.junit.runner.Result;importorg.junit.runner.notification.Failure;publicclassTestRunner{publicstaticvoidmain(String[] args){Result result =JUnitCore.runClasses(JunitAnnotation.class);for(Failure failure : result.getFailures()){System.out.println(failure.toString());}System.out.println(result.wasSuccessful());}}

运行结果

in before class
in before
in test
in after
in after class
true

观察以上的输出,这是 JUnite 执行过程:

  • beforeClass() 方法首先执行,并且仅执行一次。
  • afterClass() 方法最后执行,并且仅执行一次。
  • before() 方法在每个测试用例执行之前执行。
  • after() 方法在每个测试用例执行之后执行。
  • 在 before() 方法和 after() 方法之间,执行每个测试用例。

JUnitCore 执行测试

下面是 org.junit.runner.JUnitCore 类的声明:

publicclassJUnitCoreextendsjava.lang.Object

测试用例是使用 JUnitCore 类来执行的。JUnitCore 是运行测试的外观类。它支持运行 JUnit 4 测试。 要从命令行运行测试,可以运行 java org.junit.runner.JUnitCore 。对于只有一次的测试运行,可以使用静态方法 runClasses(Class[])。

下节介绍

【JUnit技术专题】「入门到精通系列」手把手+零基础带你玩转单元测试,让你的代码更加“强壮”(场景化测试篇)


本文转载自: https://blog.csdn.net/l569590478/article/details/135170616
版权归原作者 洛神灬殇 所有, 如有侵权,请联系我们删除。

“【JUnit技术专题】「入门到精通系列」手把手+零基础带你玩转单元测试,让你的代码更加“强壮”(实战开发篇)”的评论:

还没有评论