JUnit4学习
参考:http://www.cnblogs.com/yangxia-test/p/3996120.html
JUnit4是一个开源的java单元测试框架,我们只需要引入一个包,就可以使用它的功能
先说说他的常见注解
@Before(每个测试方法执行之前都要执行一次,比如有些数据需要重新赋值
@After(每个测试方法执行后都执行一次,比如有些数据需要重新复制
@BeforeClass(所有方法执行前执行
@AfterClass(所有方法执行后执行
@Test(timeout = xxx)看当前测试方法是否在一定时间内完成(比如看是否死循环
@Test(expected=Exception.class)测试方法中是否有抛出了这个异常
@Ignore(历史原因或者没有设置好,就先不测试这个方法,加载@Test上面,不代替@Test
下面说说使用方法
准备一个测试用的类(里面有错误的方法
package andycpp; public class Calculator {
private static int result; // 静态变量,用于存储运行结果 public void add(int n) {
result = result + n;
} public void substract(int n) {
result = result - 1; // Bug:正确的应该是result=result-n;
} public void multiply(int n) {
// 此方法尚未写好
} public void divide(int n) {
result = result / n;
} public void square(int n) {
result = n * n;
} public void squareRoot(int n) {
for (;;)
; // Bug:死循环
} public void clear() {
result = 0; // 将结果清零
} public int getResult() {
return result;
}
}
导入JUnit4包
右键项目——》构建路径——》add Library——》JUnit
在相对应的java文件右键,new——》JUnit Test Case,编写各个方法
private static Calculator calculator = new Calculator(); @Before
public void setUp() throws Exception {
calculator.clear();
} @After
public void tearDown() throws Exception {
} @Test(timeout = 1000)
public void testAdd() {
calculator.add(2);
calculator.add(3);
} @Test
public void testSubstract() {
calculator.add(10);
calculator.substract(2);
assertEquals(8, calculator.getResult());
} @Ignore("Multiply() Not yet implemented")
@Test
public void testMultiply() {
} @Test(expected = ArithmeticException.class)
public void testDivide() {
calculator.add(8);
calculator.divide(0);
assertEquals(4, calculator.getResult());
} @Test(timeout = 1000)
public void testSquareRoot() {
calculator.squareRoot(1);
}
右键测试类,run as --》JUnit可以看到效果
下面说带有多个参数一起解决
0,类前面加注解@RunWith(Parameterized.class)
1,设置参数
2,@Parameters方法
3,重写构造方法
4,@Test方法
private static Calculator calculator = new Calculator(); private int param;
private int result; @Before
public void setUp() throws Exception {
} @After
public void tearDown() throws Exception {
} @Parameters
public static Collection data() {
return Arrays.asList(new Object[][] { { 2, 4 }, { 0, 0 }, { -3, 9 } });
} public SquareTest(int param, int result) {
this.param = param;
this.result = result;
} @Test
public void testSquare() {
calculator.square(param);
assertEquals(result, calculator.getResult());
System.out.println("result=" + result);
}
效果图
我们打包想一起测试的时候,写个类包他们两个整合起来
代码内容
@RunWith(Suite.class)
@Suite.SuiteClasses({ CalculatorTest.class, SquareTest.class })
public class All {
}
JUnit4学习的更多相关文章
- Junit4学习使用和总结
Junit4学习使用和总结 部分资料来源于网络 编辑于:20190710 一.Junit注解理解 1.@RunWith 首先要分清几个概念:测试方法.测试类.测试集.测试运行器.其中测试方法就是用@T ...
- Junit4学习(一)新建Junit4工程
一,学习Junit4,学以致用 二,熟悉编写流程 工具:Eclipse,Junit包,hamcrest.core包 1,打开Eclipse开发工具,新建工程:file->Java Project ...
- Junit4学习笔记
一.初始化标注 在老Junit4提供了setUp()和tearDown(),在每个测试函数调用之前/后都会调用. @Before: Method annotated with @Before exec ...
- Junit4学习(六)Junit4参数化设置
一,背景, 有时候会对相同的代码结构做同样的操作,不同的时对参数的设置数据和预期结果:有没有好的办法提取出来相同的代码,提高代码的可重用度,junit4中使用参数化设置,来处理此种场景: 二,代码展示 ...
- JUnit4 学习笔记
一.环境搭建: 1.需要用的包: JUnit4.7:http://files.cnblogs.com/files/ShawnYang/junit4.7.zip hamcrest-1.2:http:// ...
- junit4学习(Annotation)
在一个测试类中,所有被@Test注解修饰的public,void方法都是testcase,可以被JUNIT执行. @Retention(value=RUNTIME) @Target(value=MET ...
- Junit4学习笔记--方法的执行顺序
package com.lt.Demo.TestDemo; import java.util.Arrays; import java.util.Collection; import org.junit ...
- Junit4学习(五)Junit4测试套件
一,背景 1,随着开发规模的深入和扩大,项目或越来越大,相应的我们的测试类也会越来越多:那么就带来一个问题,假如测试类很多,就需要多次运行,造成测试的成本增加:此时就可以使用junit批量运行测试类的 ...
- Junit4学习(四)Junit4常用注解
一,背景知识: 由前面的知识可以知道: /* * @Test:将一个普通方法修饰为一个测试方法 * @Test(exception=XXX.class) * @Test ...
随机推荐
- Python 面向对象的三大特性:封装,继承,多态
# 面向对象的三大特性:封装,继承,多态 # 继承的影响:资源的继承,资源的使用,资源的覆盖,资源的累加 # 资源的继承,在Python中的继承是指能使用父类的资源,而不是说在子类也复制一份父类代码到 ...
- orecle 函数
--创建函数语法 create [or replace] function [schema.]function_name (函数参数列表) --参数有IN.OUT.IN OUT三种类型:IN代表需要输 ...
- spring cloud 基本小结
Spring cloud系列十四 分布式链路监控Spring Cloud Sleuth https://blog.csdn.net/hry2015/article/details/78905489 S ...
- java 判断对象的所有属性是否为空解决方案
public static boolean allfieldIsNUll(Object o){ try{ for(Field field:o.getClass().getDeclaredFields( ...
- scala学习手记37 - 容器的使用
这次统一看一下scala中容器类的几个方法. Set filter()方法 filter()方法用来从Set中过滤获取含有指定特征的元素.示例代码如下: val colors1 = Set(" ...
- DPDK在OpenStack中的实现
随着云计算与大数据的快速发展,其对数据中心网络的性能和管理提出了更高的要求,但传统云计算架构存在多个I/O瓶颈,由于云平台基本上是采用传统的X86服务器加上虚拟化方式组建,随着40G.100G高速网卡 ...
- Apache顶级项目 Calcite使用介绍
什么是Calcite Apache Calcite是一个动态数据管理框架,它具备很多典型数据库管理系统的功能,比如SQL解析.SQL校验.SQL查询优化.SQL生成以及数据连接查询等,但是又省略了一些 ...
- JAVA中的枚举使用总结
概念 在某些情况下,一个类的对象时有限且固定的,如季节类,它只有春夏秋冬4个对象这种实例有限且固定的类,在 Java 中被称为枚举类; 理解 类里面定义了固定数量的实例,类名如同命令空间 代码 pac ...
- 计算从哪天起应该购买预售火车票.cs
代码直接CSC编译即可. 计算从哪天起应该购买预售火车票.cs using System; using System.Diagnostics; using System.IO; class Progr ...
- IOS-SQLite3的封装
IWStudent.h // // IWStudent.h // 02-SQLite的封装 // // Created by apple on 14-5-22. // Copyright (c) 20 ...