JUnit4中使用Hamcrest测试框架的assertThat断言 小实例
?1.JUnit4.4引入了Hamcrest框架,Hamcest提供了一套匹配符Matcher,这些匹配符更接近自然语言,可读性高,更加灵活;
2.使用全新的断言语法:assertThat,结合Hamcest提供的匹配符,只用这一个方法,就可以实现所有的测试;
3.assertThat语法如下:
assertThat(T actual, Matcher<T> matcher);
assertThat(String reason, T actual, Matcher<T> matcher);
其中actual为需要测试的变量,matcher为使用Hamcrest的匹配符来表达变量actual期望值的声明;
4.注意事项:
a.必须导入JUnit4.4之后的版本才能使用assertThat方法;
b.不需要继承TestCase类,但是需要测试方法前必须加“@Test”。
实例:
public class C {
public int add(int a, int b) {
return a + b;
}
public double div(double a, double b) {
return a / b;
}
public String getName(String name) {
return name;
}
public List<String> getList(String item) {
List<String> l = new ArrayList<String>();
l.add(item);
return l;
}
public Map<String, String> getMap(String key, String value) {
Map<String, String> m = new HashMap<String, String>();
m.put(key, value);
return m;
}
junit 断言 assertThat 2011 年 06 月 28 日
使用junit进行单元测试的简单例子,其中包括hamcrest和注解的方法。myEclipse 下使用...Junit4 的新特性 1、Hamcrest 的断言语法 、 的断言语法——assertThat 1.导入...
简介 JUnit 是一个开放源代码的 JAVA 测试框架, 用于编写和运行可重复的测试。...assertThat 使用了 Hamcrest 的 Matcher 匹配符,用户可以使用匹配符规定的匹配准 ...
本文通过理论分析和详细例子向读者阐述 JUnit 4.4 所带来的最新特性,读者通过...assertThat 一个断言语句,结合 Hamcrest 提供的匹配符,就可以表达全部的测试思想....
四、放弃旧的断言,使用 Junit 的 hamcrest 断言 1、断言:Assert,帮助文档。F:\测试工具\junit4.8\javadoc\index.html junit4.8.zip assertThat():几乎可以...
Junit4 单元测试 概念简单的说就是:写了个类,要给别人用,会不会有 bug?...放弃旧的断言,使用 hamcrest 断言导入 使用里面的 is()方法 assertThat(比较的...
测试方法的调用顺序为: @Before –> @Test –> @After JUnit 主要有以下断言...assertFalse(布尔条件),检查布尔条件是否为假 assertThat 与 org.hamcrest....
JUnit4-为什么要进行单元测试_自然科学_专业资料。JUnit4-为什么要进行单元测试...2. 3. assertThat 使用 hamcrest 的匹配方法 a) 更自然 示例 a) assertThat(...
Junit4 的新特性 1、Hamcrest 的断言语法——assertThat 1.导入 junit 4 和 ...containsString 匹配符表明如果测试的字符串 testedString 包含子字符串"developer...
? ? ? ? Selenium介绍 Selenium Java测试准备及测试用例 具体例子 后续方向 ...format assertThat ? Junit4结合Hamcrest提供了新的断言语句-assertThat,只 需一...
热门文档