要点:
注解 断言 参数化 套件 (testSuite)
一, 引入 Junit4 单元测试框架
项目上单击右键 ->new->other->JUnit->Junit Test Case->Next->New Juint 4 test ->Name->setup->teardown->Class Under Test->Next-> 勾选你要测试的函数 ->Finish-> 提示引入 JUnit 库 ->Finish
二, 简单的实例代码
1. 被测试类:
- package com.zhang.java;
- public class LoginSample {
- public LoginSample() { // 构造方法
- System.out.println("构造方法被调用!");
- }
- public boolean login(String ln, String pwd) {
- if (ln != null && ln.length()> 2 && ln.length() <17 && pwd != null && pwd.length()> 2 && pwd.length() <17) {
- if (ln.equals("zzp") && pwd.equals("123456")) {
- System.out.println("恭喜您, 登录成功!");
- return true;
- } else {
- System.out.println("用户名或密码错误!");
- }
- } else {
- System.out.println("参数错误!");
- }
- return false;
- }
- }
2, 测试类代码:
- package com.zhang.java;
- import static org.junit.Assert.*;
- import org.junit.After;
- import org.junit.AfterClass;
- import org.junit.Before;
- import org.junit.BeforeClass;
- import org.junit.Test;
- public class LoginSampleTest {
- /**
- * BeforeClass 和 Before 做测试环境的初始化;
- * AfterClass 和 After 做数据的清理扫尾工作 (如关闭数据库连接)
- */
- private LoginSample login;
- // 在测试类执行之前执行的方法
- @BeforeClass
- public static void setUpBeforeClass() throws Exception {
- System.out.println("@BeforeClass 方法被执行");
- }
- // 在测试类执行之后执行的方法
- @AfterClass
- public static void tearDownAfterClass() throws Exception {
- System.out.println("@AfterClass 方法被执行");
- }
- @Before
- public void setUp() throws Exception {
- System.out.println("@Before 被调用 %%%%%%%%%%%%");
- login = new LoginSample();
- }
- @After
- public void tearDown() throws Exception {
- System.out.println("@After 被调用 &&&&&&&&&&&&&&&&&");
- }
- // 在一个测试类中可以有多个 @test 注解的测试方法
- @Test
- public void testLogin() {
- System.out.println("testLogin() 方法被调用");
- // fail("Not yet implemented");
- boolean result = login.login("zzp", "123456");
- if(result)
- System.out.println("测试通过!");
- else
- fail("测试失败!"); // 用到测试框架的断言 fail() 方法
- System.out.println("===========");
- }
- }
测试类运行结果:
@BeforeClass 方法被执行
@Before 被调用 %%%%%%%%%%%%
构造方法被调用!
testLogin() 方法被调用
恭喜您, 登录成功!
测试通过!
===========
@After 被调用 &&&&&&&&&&&&&&&&&
@AfterClass 方法被执行
三, 补充说明
四, Junit 参数化数据驱动
1, 相关注解:
@RunWith(Parameterized.class): 注解 JUnit 测试类, 指定该测试类使用参数化方式运行, 要写在测试类外面 (不是类里面);
@Parameters: 生成并返回测试数据, 一般使用 Object[][] 二位数组来接收数据返回. 该方法必须是静态方法, 因为要在测试方法运行之前生成测试数据.
2, 相关方法:
在 JUnit 参数化的类中, 需要有一个构造方法, 用于存储和传递测试数据. 由 @Parameters 注解的静态方法, 是一个 Collection 或者 Iterator, 生成并返回测试数据.
构造方法需要用对应的参数列表接收数据. 这样, 数据将被传递到 @Test 注解的测试方法, 逐条进行测试, 直到集合中不再有元素为止.
3, 代码展示
a, 被测试类代码
- package com.zhang.java;
- public class Plus {
- public int Sum(int x, int y) {
- int result=x+y;
- return result;
- }
- }
b, 单元测试用例代码
- package com.zhang.java;
- import static org.junit.Assert.*;
- import org.junit.Before;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.junit.runners.Parameterized;
- import org.junit.runners.Parameterized.Parameters;
- // 写在测试类前, 表示该测试类用参数化形式运行.
- @RunWith(Parameterized.class)
- public class PlusTest {
- // 用成员变量接收来自 parameter 方法生成的数据
- // 测试名
- public String caseName;
- // 测试预期结果
- public String exResult;
- // 测试用到的数据
- public int firstNum;
- public int secondNum;
- private Plus p;
- // 测试前的准备, 初始化被测试类.
- @Before
- public void setUp() throws Exception {
- p=new Plus();
- }
- // 构造函数用于接收数据 parameter 的数据, 并给成员变量赋值.
- public PlusTest(String input1,String input2,int input3 ,int input4) {
- caseName=input1;
- exResult=input2;
- firstNum=input3;
- secondNum=input4;
- }
- @Parameters(name="{0},res:{1},operater:sum({2}+{3})")
- public static Object[][] setData(){
- return new Object[][] {
- {"左边界 - 1","-2147483649",-2147483648,-1}, //Object[0][]
- {"左边界 + 0","-2147483648",-2147483648,0}, //Object[1][]
- {"左边界 + 1","-2147483647",-2147483648,1} //Object[2][]
- };
- }
- // 每一条数据都会用于执行一次 @Test 测试方法
- @Test
- public void testSumIntInt() {
- System.out.println(caseName);
- // 用到的方法:
- //void org.junit.Assert.assertEquals(String message, Object expected, Object actual)
- assertEquals(caseName,exResult, Integer.toString(p.Sum(firstNum, secondNum)));
- }
- }
运行后的结果
左边界 - 1
左边界 + 0
左边界 + 1
五, TestSuite 测试套件
创建方法:
在包含 JUnit4 case 的包中右键 ->new->other->JUnit->Junit Test Suite>Next ->New JUnit4 Suite->Name-> 选择你需要执行的 Case->Finish.
添加 case:
在 @SuiteClasses 注解中指定要执行的 case, 以 classname.class 的形式添加, 如下:
@SuiteClasses({ JUnit4.class })
来源: http://www.bubuko.com/infodetail-2935227.html