hamcrest说明
Java Hamcrest官方网站,目前的版本是1.3
JUnit和Hamcrest是不同的框架。但是JUnit使用了Hamcrest。使用Hamcrest可以写出更为简洁的Test代码
Hamcrest 是一个测试的框架,它提供了一套通用的匹配符 Matcher,灵活使用这些匹配符定义的规则,程序员可以更加精确的表达自己的测试思想,指定所想设定的测试条件。
全新的断言语法assertThat,更符合口语的习惯
使用
- 首先需要加载Hamcrest,在JUnit加载的时候,已经加载hamcrest-core,但是还有很多包放置在hamcrest-library中,所以需要在Maven配置中增加如下内容
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
注意:曾经在运行中发现有下面的问题
java.lang.NoSuchMethodError: org.hamcrest.core.AllOf.allOf(Lorg/hamcrest/Matcher;Lorg/hamcrest/Matcher;)Lorg/hamcrest/Matcher;
at org.hamcrest.Matchers.allOf(Matchers.java:33)
at com.yun.test.sample.aasert.AssertThatTest.testAdd(AssertThatTest.java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
解决方案:吧上面的Maven配置项放置在JUnit配置之前即可!
- 参考下面的例子,注意包的引用
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.util.List;
import java.util.Map;
import org.junit.Test;
public class CTest {
@Test
public void testAdd() {
// 一般匹配符
int s = new C().add(1, 1);
// allOf:所有条件必须都成立,测试才通过
assertThat(s, allOf(greaterThan(1), lessThan(3)));
// anyOf:只要有一个条件成立,测试就通过
assertThat(s, anyOf(greaterThan(1), lessThan(1)));
// anything:无论什么条件,测试都通过
assertThat(s, anything());
// is:变量的值等于指定值时,测试通过
assertThat(s, is(2));
// not:和is相反,变量的值不等于指定值时,测试通过
assertThat(s, not(1));
// 数值匹配符
double d = new C().div(10, 3);
// closeTo:浮点型变量的值在3.0±0.5范围内,测试通过
assertThat(d, closeTo(3.0, 0.5));
// greaterThan:变量的值大于指定值时,测试通过
assertThat(d, greaterThan(3.0));
// lessThan:变量的值小于指定值时,测试通过
assertThat(d, lessThan(3.5));
// greaterThanOrEuqalTo:变量的值大于等于指定值时,测试通过
assertThat(d, greaterThanOrEqualTo(3.3));
// lessThanOrEqualTo:变量的值小于等于指定值时,测试通过
assertThat(d, lessThanOrEqualTo(3.4));
// 字符串匹配符
String n = new C().getName("Magci");
// containsString:字符串变量中包含指定字符串时,测试通过
assertThat(n, containsString("ci"));
// startsWith:字符串变量以指定字符串开头时,测试通过
assertThat(n, startsWith("Ma"));
// endsWith:字符串变量以指定字符串结尾时,测试通过
assertThat(n, endsWith("i"));
// euqalTo:字符串变量等于指定字符串时,测试通过
assertThat(n, equalTo("Magci"));
// equalToIgnoringCase:字符串变量在忽略大小写的情况下等于指定字符串时,测试通过
assertThat(n, equalToIgnoringCase("magci"));
// equalToIgnoringWhiteSpace:字符串变量在忽略头尾任意空格的情况下等于指定字符串时,测试通过
assertThat(n, equalToIgnoringWhiteSpace(" Magci "));
// 集合匹配符
List<String> l = new C().getList("Magci");
// hasItem:Iterable变量中含有指定元素时,测试通过
assertThat(l, hasItem("Magci"));
Map<String, String> m = new C().getMap("mgc", "Magci");
// hasEntry:Map变量中含有指定键值对时,测试通过
assertThat(m, hasEntry("mgc", "Magci"));
// hasKey:Map变量中含有指定键时,测试通过
assertThat(m, hasKey("mgc"));
// hasValue:Map变量中含有指定值时,测试通过
assertThat(m, hasValue("Magci"));
}
}
assertThat是JUnit提供的,它与Hamcrest组件进行配合实现测试工作。
自定义扩展
当有需要的时候,可以自定义匹配器。这里演示一个自定义判断数字范围的例子
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
public class IsNotRangeNumber extends TypeSafeMatcher<Double>{
private Double start ;
private Double end;
private IsNotRangeNumber(Double start,Double end){
this.start = start;
this.end = end;
}
@Override
public void describeTo(Description description) {
description.appendText("这个数字不在指定范围内");
}
@Override
protected boolean matchesSafely(Double item) {
boolean flag = false;
if(item != null && start <= item && item <= end){
flag = true;
}
return flag;
}
public static Matcher<Double> notRangeNumber(Double a,Double b){
return new IsNotRangeNumber(a,b);
}
}
使用方法
assertThat(22.9,IsNotRangeNumber.notRangeNumber(12.0, 20.0));
网友评论