JUnit入门教程(二)
来源:互联网 发布:武汉广电网络客服电话 编辑:程序博客网 时间:2024/06/08 08:45
一:介绍
接着上次的课程,今天我们学习JUnit4的新特性assertThat()方法,这种方式和其余的assert方法比起来,更加接进英语.
二:分析API
API中的例子:
参数
T Actual:你要计算比较的值
org.hamcrest.Matcher<T> matcher:规则匹配器
这个匹配器类是org.hamcrest.Matcher,该类下有许多用于比较计算的静态方法,这个包JUnit中是没有的,要去Hamcrest官网下载,具体链接可以自己百度或者看我上篇文章的链接。
三:使用AssertThat()测试四则运算
Calculator类代码:
package com.ksjl.junit4;/** * @author 旷世绝伦 * * * */public class Calculator { // 四则运算public int plus(int x, int y) {return x + y;}public int subtraction(int x, int y) {return x - y;}public int multiplication(int x, int y) {return x * y;}public double division(double x, double y) {return x / y;}}
测试类:
package com.ksjl.junit4.test;import org.junit.BeforeClass;import org.junit.Test;import static org.junit.Assert.*;//注意这边是静态引入import static org.hamcrest.Matchers.*;import com.ksjl.junit4.Calculator;/** * @author 旷世绝伦 * * * */public class CalculatorTest {private static Calculator cal = null;@BeforeClasspublic static void beforeClass() {cal = new Calculator();}@Testpublic void testAll() {int result = cal.plus(5, 5); // 加法assertThat(result, is(10));result = cal.subtraction(5, 5);// 减法assertThat(result, greaterThanOrEqualTo(0));result = cal.multiplication(5, 5);// 乘法assertThat(result, allOf(lessThan(30), greaterThan(20))); // 满足两个条件double result2 = cal.division(10.0, 3.0);// 除法assertThat(result2, closeTo(3.3, 0.3)); //判断是否在3.3 左右 误差不超过正负0.3}}
工程目录,大家看下,一共要导3个包,如下:
运行测试,看到绿色就OK了,是不是So Easy?
四:添加一个Student类(同时测试多个类,体现使用JUnit的好处)
Student代码:
package com.ksjl.junit4;/** * @author 旷世绝伦 * * */public class Student {private String name="tom";public String getName() {return name;}}
测试类代码:
package com.ksjl.junit4.test;import org.junit.BeforeClass;import org.junit.Test;import static org.hamcrest.Matchers.*;import static org.junit.Assert.*;import com.ksjl.junit4.Student;/** * @author 旷世绝伦 * * */public class StudentTest {private static Student student = null;@BeforeClasspublic static void beforeClass() {student = new Student();}@Testpublic void testAll() {String name = student.getName();assertThat(name, is("tom"));assertThat(name, not("to"));assertThat(name, containsString("to"));// 是否包含toassertThat(name, startsWith("t"));assertThat(name, endsWith("m"));assertThat(name, equalTo("tom"));assertThat(name, equalToIgnoringCase("Tom"));assertThat(name, equalToIgnoringWhiteSpace("tom"));//忽略空格是否等于tom}}
工程目录如下:
这时候我们点击com.ksjl.junit.test包Run As Junit Test 就能同时测试两个类,没问题,全部正确。
大家是否感到了新特性的好处呢?
五:其他
1. org.hamcrest.Matchers类中有很多方法,我就不一一说了,例如能测试Map和List ,大家自己看API,自己做实验,API在哪看?
找到hamcrest-library-1.3-javadoc.jar 解压 ,目录下index.html就是API
2. 如果遇到ClassLoader的错误,这是因为类加载器不同的原因,请将jar包像我一样布置,就没问题了.
六:总结
更详细的资料请自己查阅API,入门教程到此结束,算是带大家粗略的认识了下JUnit,谢谢大家!
- JUnit入门教程(二)
- JUnit入门教程(一)
- junit(二)
- JUnit实战篇 (二)
- 【转】JUnit(二)
- junit浅学笔记二
- junit浅学笔记二
- Junit学习笔记(二)
- Junit 入门(二)
- junit浅学笔记二
- (二)Junit单元测试
- Junit使用指南(二)
- Junit 测试《二》
- Junit原理详解二
- JUnit笔记二
- Java之------JUnit(二)
- JUnit使用(二)
- 日常笔记------JUnit(二)
- 伪教练技术培训之殇-2013年9月江西IDC拓行榜与综述
- Python学习笔记4--一个实例(塔防游戏)
- hdu2553解题报告
- C++ MFC学习笔记三
- HTTP协议——代理
- JUnit入门教程(二)
- 对新手有用的JavaScript开发小建议
- [安卓破解]快手看片,破解版,无广告
- IIS处理并发请求时出现的问题及解决
- QFileDialog
- NS2中接收信号强度获取
- 会话技术:session 和 cookie
- HTTP协议——状态码详解
- <Win32_17>集音频和视频播放功能于一身的简易播放器