Junit的使用
Junit是用于编写单元测试的框架。对于已经写好的函数,可以使用Junit生成单元测试代码。
自己的环境是:Linux
Java环境是:JDK1.7
IDE:Eclipse Java EE IDE for Web Developers.Version: Mars.2 Release (4.5.2) Build id: 20160218-0600
使用步骤
1、新建项目,命名为JUnit_Test,编写了一个用于计算的类:Calcutor,代码中故意留下一些问题,便于测试。代码如下:
package com.test.unit;
public class Calculator
{
// 加法
public int add(int a, int b)
{
return a + b;
}
// 减法
public int substract(int a, int b)
{
return a + b; // Bug: 正确的应该是a-b
}
// 乘法
public int multiply(int a, int b)
{
try
{
Thread.sleep(300);//用于性能测试
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return a * b;
}
// 除法
public int divide(int a, int b) throws ArithmeticException
{
if (b == 0)
{
throw new ArithmeticException();
}
return a / b;
}
// 平方根
public void squareRoot(int n)
{
for (;;)
; // Bug : 死循环
}
}
Calculator
、引入Junit库。选中项目,右键——Build Path——Add Libraries,选择JUnit,引入的包如下:
、生成测试类。选中
Calcutor.java,右键——New——JUnit
Test Case
选择setUp,再下一步,选择要进行测试的方法,及生成了选择的函数的测试用例,再根据需要自己编写代码。
主要测试类型有三种:
(1)常规的计算测试。调用函数计算值,输入自己期望的值,以及出错信息。如果调用函数计算的值与自己期望的值不相等,则抛出出错信息。
毫秒里运行完,如果这个方法200毫秒不能运行完,那么这个方法就应该抛出异常。
(3)异常测试。期待抛出异常,如果没有抛出异常,则报错。
测试代码如下:
package com.test.unit;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
public class CalculatorTest
{
Calculator cal;
@Before
public void setUp() throws Exception
{
cal = new Calculator();
}
@SuppressWarnings("deprecation")
@Test
public void testAdd()
{
int rel = cal.add(10, 5);//调用函数计算的值
assertEquals("加法有问题", 15, rel);//传入计算的值,自己期望的值,出错信息
}
@Test
public void testSubstract()
{
int rel = cal.substract(20, 5);
assertEquals("减法有问题", 16, rel);
}
// 用于性能测试
// 要让一个方法,200毫秒里运行完,如果这个方法200毫秒不能运行完,那么这个方法就应该抛出异常
@Test(timeout = 200)
public void testMultiply()
{
int rel = cal.multiply(2, 4);
assertEquals("乘法有问题", 8, rel);
}
//异常测试
@Test(expected = ArithmeticException.class)
public void testDivide()
{
int rel = cal.divide(10, 0);
assertEquals("除法有问题", 5, rel);
}
}
CalculatorTest
、完成测试代码的编写后,包管理器中选中CalculatorTest,右键——Run as——JUnit Test,进行测试,测试结果如下:
根据报错信息再修改自己的代码。
Junit的使用的更多相关文章
- 记一个mvn奇怪错误: Archive for required library: 'D:/mvn/repos/junit/junit/3.8.1/junit-3.8.1.jar' in project 'xxx' cannot be read or is not a valid ZIP file
我的maven 项目有一个红色感叹号, 而且Problems 存在 errors : Description Resource Path Location Type Archive for requi ...
- 「译」JUnit 5 系列:条件测试
原文地址:http://blog.codefx.org/libraries/junit-5-conditions/ 原文日期:08, May, 2016 译文首发:Linesh 的博客:「译」JUni ...
- AndroidStudio — Error:Failed to resolve: junit:junit:4.12错误解决
原博客:http://blog.csdn.net/u013443865/article/details/50243193 最近使用AndroidStudio出现以下问题: 解决:打开app下的buil ...
- 「译」JUnit 5 系列:环境搭建
原文地址:http://blog.codefx.org/libraries/junit-5-setup/ 原文日期:15, Feb, 2016 译文首发:Linesh 的博客:环境搭建 我的 Gith ...
- [深入JUnit] 测试运行的入口
阅读前提 了解JUnit 对JUnit的内部实现有兴趣 不妨看看[深入JUnit] @Before, @After, @Test的秘密] 代码版本: junit 4.12代码搜索工具: http:// ...
- 「译」JUnit 5 系列:扩展模型(Extension Model)
原文地址:http://blog.codefx.org/design/architecture/junit-5-extension-model/ 原文日期:11, Apr, 2016 译文首发:Lin ...
- 「译」JUnit 5 系列:架构体系
原文地址:http://blog.codefx.org/design/architecture/junit-5-architecture/ 原文日期:29, Mar, 2016 译文首发:Linesh ...
- 「译」JUnit 5 系列:基础入门
原文地址:http://blog.codefx.org/libraries/junit-5-basics/ 原文日期:25, Feb, 2016 译文首发:Linesh 的博客:JUnit 5 系列: ...
- 新手入门JUnit单元测试
首先将JUnit插件安装到Eclipse或myeclipse里面,编写完一个模块或者实体类的时候,直接右击,new一个JUnit项目,选择你想测试的实体类(模块),然后会自动生成一个类,这个类,我们将 ...
- [Android]使用自定义JUnit Rules、annotations和Resources进行单元测试(翻译)
以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/5795091.html 使用自定义JUnit Rules.ann ...
随机推荐
- Mybatis常用总结:参数,返回,执行sql,include等
1.参数注入1.1用#{0},#{1}的形式,0代表第一个参数,1代表第二个参数 public List<RecordVo> queryList(String workerId, Inte ...
- Javascript前端和JAVA后端对加密库的处理实例
前端加密 Javascript的加解密有开源的库,http://www.oschina.net/p/crypto-js/ 如下是具体的使用例子 <!DOCTYPE html> <ht ...
- Android中的自定义控件(一)
自定义控件是根据自己的需要自己来编写控件.安卓自带的控件有时候无法满足你的需求,这种时候,我们只能去自己去实现适合项目的控件.同时,安卓也允许你去继承已经存在的控件或者实现你自己的控件以便优化界面和创 ...
- css小技巧(1)
1.-webkit-overflow-scrolling: touch; 解决ios滑动时无缓冲问题 2.::-webkit-scrollbar 设置ios滑动时是否显示滚动条 3.::selecti ...
- Windows Installer 服务启动错误 14007 的解决办法
问题: 在 本地计算机 无法启动 Windows Installer 服务. 错误代码 14007: 在活动的激活上下文中没有找到任何查找密钥. 这个问题似乎涉及到 Windows Installer ...
- Ajax语法浅析
Ajax是目前很普遍的一门技术,也是很值得探讨和研究的一门技术.本文将针对Ajax的发展过程并结合其在不同库框架中的使用方式来和大家分享下Ajax的那些新老语法. Ajax简介 Ajax全称为“Asy ...
- 前端之float的几种清除浮动方式
前端之float的几种清除浮动方式 本节内容 1.float清除方式1 2.float清除方式2 3.float清除方式3 4.float清除方式4 1.float清除方式1 <!DOCTYPE ...
- polya/burnside 学习
参考链接: http://www.cnblogs.com/hankers/archive/2012/08/03/2622231.html http://blog.csdn.net/raalghul/a ...
- Lua面线对象学习
--[[ ]] local userdata = {} local Register = {} function Register:del(key) userdata[self.namespace][ ...
- SQL用法操作合集
SQL用法操作合集 一.表的创建 1.创建表 格式: 1 CREATE TABLE 表名 2 (列名 数据类型(宽度)[DEFAULT 表达式][COLUMN CONSTRAINT], 3 ... ...