当前位置: 代码迷 >> 综合 >> JUnit 测试
  详细解决方案

JUnit 测试

热度:18   发布时间:2024-01-26 16:50:00.0

简介:

JUnit是一个java语言的单元测试框架。所谓单元测试是测试应用程序的功能是否能够按需要正常运行。单元测试是一个对单一实体(类或方法)的测试。多数Java的开发工具都集成了JUnit单元测试工具。

特点:

  • JUnit是一个开放的资源框架,用于编写和运行测试。
  • JUnit有效提高代码质量。
  • 提供注释来识别测试方法。
  • 提供断言来测试预期结果。
  • 提供测试运行来运行测试。

系统要求:

  • JUnit 是java的一个框架,所以我们需要安装JDK(JDK1.5或以上)。
  • JDK1.5及以上版本开始支持JUnit4,JUnit采用注解的方式实现测试代码的编写。

JUnit PAI:

JUnit 所有核心类都包含在 junit.framework 包下:

JUnit核心类
名称 功能
Assert 提供了一系列的编写测试的声明方法
TestCase 定义了运行多重测试的固定格式
TestResult TestResult 类收集所有执行测试案例的结果
TestSuite TestSuite 是测试的集合

1、Assert 类

org.junit.Assert :public class Assert extends java.lang.Object 提供了一系列的编写测试的声明方法。

org.junit.Assert
序号 方法 描述
1 void assertEquals(boolean expected, boolean actual) 检查两个变量或者等式是否平衡
2 void assertFalse(boolean condition)  检查条件是否为假
3 void assertTrue(boolean condition)  检查条件是否为真
4 void assertNotNull(Object object)  检查对象是否不为空
5 void assertNull(Object object) 检查对象是否为空
6 void fail()  在没有报告的情况下使测试不通过
7 void assertArrayEquals(expectedArray, resultArray) 检查两个数组是否相等

 

2、TestCase 类

org.junit.TestCaset :public abstract class TestCase extends Assert implements Test

org.junit.TestCase
序号 方法 描述
1 int countTestCases() 为被run(TestResult result) 执行的测试案例计数
2 TestResult createResult() 创建一个默认的 TestResult 对象
3 String getName() 获取 TestCase 的名称
4 TestResult run() 收集由TestResult 对象产生的结果
5 void run(TestResult result) 在 TestResult 中运行测试案例并收集结果
6 void setName(String name) 设置 TestCase 的名称
7 void setUp() 创建固定装置,例如,打开一个网络连接
8 void tearDown() 拆除固定装置,例如,关闭一个网络连接
9 String toString() 返回测试案例的一个字符串表示

3、TestResult 类

org.junit.TestResult :public class TestResult extends Object

org.junit.TestResult
序号 方法 描述
1 void addError(Test test, Throwable t) 在错误列表中加入一个错误
2 void addFailure(Test test, AssertionFailedError t) 在失败列表中加入一个失败
3 void endTest(Test test) 显示测试被编译的这个结果
4 int errorCount() 获取被检测出错误的数量
5 Enumeration errors() 返回错误的详细信息
6 int failureCount() 获取被检测出的失败的数量
7 void run(TestCase test) 运行 TestCase
8 int int runCount() 获得运行测试的数量
9 void startTest(Test test) 声明一个测试即将开始
10 void stop() 标明测试必须停止

 

4、TestSuite 类

org.junit.TestSuite :public class TestSuite extends Object implements Test

org.junit.TestSuite
序号 方法 描述
1 void addTest(Test test)  在套中加入测试。
2 void addTestSuite(Class<? extends TestCase> testClass) 将已经给定的类中的测试加到套中
3 int countTestCases() 对这个测试即将运行的测试案例进行计数
4 String getName() 返回套的名称。
5 void run(TestResult result) 在 TestResult 中运行测试并收集结果。
6 void setName(String name) 设置套的名称。
7 Test testAt(int index) 在给定的目录中返回测试
8 int testCount() 返回套中测试的数量。
9 static Test warning(String message) 返回会失败的测试并且记录警告信息。

注释

JUnit 为我们提供了测试相关注释。

JUnit常用注释
序号 注释 描述
1 @Test 应用在 public void 修饰的方法上,标记该方法为一个测试案例 。
2 @Before 应用在 public void  修饰的方法上,对每一个测试用例执行,在每一个方法执行之前执行。
3 @After 应用在 public void  修饰的方法上,对每一个测试用例执行,在每个方法之后执行。
4 @BeforeClass 在应用在 public void  修饰的方法上,标记该方法需要在类中所有方法前运行。
5 @AfterClass 在应用在 public void  修饰的方法上,它将会使方法在所有测试结束后执行(可以用来进行清理活动)。
6 @Ignore 该注释被应用后,方法将被忽略。

示例:

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@BeforeClasspublic static void beforeClass() {System.out.println("in before class");}//execute after class@AfterClasspublic static void  afterClass() {System.out.println("in after class");}//execute before test@Beforepublic void before() {System.out.println("in before");}//execute after test@Afterpublic void after() {System.out.println("in after");}//test case@Testpublic void test() {System.out.println("in test");}//test case ignore and will not execute@Ignorepublic void ignoreTest() {System.out.println("in ignore test");}
}

结果:

检查运行结果

in before class
in before
in test
in after
in after class
  相关解决方案