Junit4 断言新方法
Posted R.Z
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Junit4 断言新方法相关的知识,希望对你有一定的参考价值。
话不多少说,直接上代码
package ASSERTTEST;
import org.junit.Assert;
import org.hamcrest.*;
import org.junit.Test;
import static org.junit.Assert.*;
import java.util.List;
import java.util.Map;
import static org.hamcrest.Matchers.*;
public class MYTEST {
@Test
public void testAdd() {
//一般匹配符
int s = new T().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 = 5.0;
//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 T().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 T().getList("Magci");
//hasItem:Iterable变量中含有指定元素时,测试通过
assertThat(l, hasItem("Magci"));
Map<String, String> m = new T().getMap("mgc", "Magci");
//hasEntry:Map变量中含有指定键值对时,测试通过
assertThat(m, hasEntry("mgc", "Magci"));
//hasKey:Map变量中含有指定键时,测试通过
assertThat(m, hasKey("mgc"));
//hasValue:Map变量中含有指定值时,测试通过
assertThat(m, hasValue("Magci"));
}}
最坑的就是这几个jar 的版本了,,
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.5</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-core -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3.RC2</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-library -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3.RC2</version>
<scope>test</scope>
</dependency>
以上是关于Junit4 断言新方法的主要内容,如果未能解决你的问题,请参考以下文章