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

实战开发篇

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

创建Pojo模型

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

EmployeeDetails 类被用于

  • 取得或者设置雇员的姓名的值。
  • 取得或者设置雇员的每月薪水的值。
  • 取得或者设置雇员的年龄的值。
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;
	}
}

EmpBusinessLogic 类被用来计算:

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

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

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;
		}
	}
}

创建一个名为 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);
	   }
	}

创建一个名为 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());
	}
}

断言操作

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

public class Assert extends java.lang.Object

该类提供了许多有用的断言方法,可用于编写测试用例。仅记录失败的断言。以下是Assert类中一些有用的方法的示例:

序号 方法和描述
1 void assertEquals(boolean expected, boolean actual) 检查两个变量或者等式是否成立
2 void assertTrue(boolean expected, boolean actual) 检查条件为真)
3 void assertFalse(boolean condition) 检查条件为假
4 void assertNotNull(Object object) 检查对象不为空
5 void assertNull(Object object) 检查对象为空
6 void assertSame(boolean condition) assertSame() 方法检查两个相关对象是否指向同一个对象
7 void assertNotSame(boolean condition) assertNotSame() 方法检查两个相关对象是否不指向同一个对象
8 void assertArrayEquals(expectedArray, resultArray) assertArrayEquals() 方法检查两个数组是否相等

下面我们在例子中试验一下上面提到的各种方法。创建一个文件名为 TestAssertions.java 的类

import org.junit.Test;
import static org.junit.Assert.*;
public class TestAssertions {
	@Test
	public void testAssertions() {
		//test data
		String str1 = new String ("abc");
		String str2 = new String ("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 equal
		assertEquals(str1, str2);
		//Check that a condition is true
		assertTrue (val1 < val2);
		//Check that a condition is false
		assertFalse(val1 > val2);
		//Check that an object isn't null
		assertNotNull(str1);
		//Check that an object is null
		assertNull(str3);
		//Check if two object references point to the same object
		assertSame(str4,str5);
		//Check if two object references not point to the same object
		assertNotSame(str1,str3);
		//Check whether two arrays are equal to each other.
		assertArrayEquals(expectedArray, resultArray);
	}
}

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

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class TestRunner2 {
public static void main(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 的类来测试注释

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
public class JunitAnnotation {
	//execute before class
	@BeforeClass
	public static void beforeClass() {
		System.out.println("in before class");
	}
	//execute after class
	@AfterClass
	public static void afterClass() {
		System.out.println("in after class");
	}
	//execute before test
	@Before
	public void before() {
		System.out.println("in before");
	}
	//execute after test
	@After
	public void after() {
		System.out.println("in after");
	}
	//test case
	@Test
	public void test() {
		System.out.println("in test");
	}
	//test case ignore and will not execute
	@Ignore
	public void ignoreTest() {
    	System.out.println("in ignore test");
    }
}

创建一个文件名为 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(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 类的声明:

public class JUnitCore extends java.lang.Object

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

下节介绍

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

猜你喜欢

转载自juejin.im/post/7240486780578021437