JUnit之断言assert
来源:互联网 发布:windows不显示桌面 编辑:程序博客网 时间:2024/04/28 09:28
一、简介
- JUnit4.4引入了Hamcrest框架,Hamcest提供了一套匹配符Matcher,这些匹配符更接近自然语言,可读性高,更加灵活;
- 使用全新的断言语法:assertThat,结合Hamcest提供的匹配符,只用这一个方法,就可以实现所有的测试;
二、语法介绍
assertThat语法如下:
assertThat(T actual, Matcher<T> matcher); assertThat(String reason, T actual, Matcher<T> matcher);
其中actual为需要测试的变量,matcher为使用Hamcrest的匹配符来表达变量actual期望值的声明;
实例:
assertThat(frank.changeUserInfo(),equalTo("男"));
注意事项:
1. 必须导入JUnit4.4之后的版本才能使用assertThat方法;2. 不需要继承TestCase类,但是需要测试方法前必须加“@Test”
三、Assert类的类图结构
四、一般匹配方法
- 要求所有的条件都要通过测试才算成功
assertThat( testedNumber, allOf( greaterThan(8), lessThan(16) ) );
配符表明如果接下来的所有条件必须都成立测试才通过,相当于“与”(&&) - 接下来的所有条件只要有一个成立则测试通过
assertThat( testedNumber, anyOf( greaterThan(16), lessThan(8) ) );
注释:anyOf匹配符表明如果接下来的所有条件只要有一个成立则测试通过,相当于“或”(||) - 无论什么条件,永远为true
assertThat( testedNumber, anything() );
注释:anything匹配符表明无论什么条件,永远为true - 等于判断
assertThat( testedString, is( “developerWorks” ) );
注释: is匹配符表明如果前面待测的object等于后面给出的object,则测试通过 - 取反判断
assertThat( testedString, not( “developerWorks” ) );
注释:not匹配符和is匹配符正好相反,表明如果前面待测的object不等于后面给出的object,则测试通过
四、字符串相关匹配符
- 包含字符串
assertThat( testedString, containsString( “developerWorks” ) );
注释:containsString匹配符表明如果测试的字符串testedString包含子字符串”developerWorks”则测试通过 - 以指定字符串结尾
assertThat( testedString, endsWith( “developerWorks” ) );
注释:endsWith匹配符表明如果测试的字符串testedString以子字符串”developerWorks”结尾则测试通过 - 以指定字符串开始
assertThat( testedString, startsWith( “developerWorks” ) );
注释:startsWith匹配符表明如果测试的字符串testedString以子字符串”developerWorks”开始则测试通过 - 字符串相等测试
assertThat( testedValue, equalTo( expectedValue ) );
注释: equalTo匹配符表明如果测试的testedValue等于expectedValue则测试通过,equalTo可以测试数值之间,字符串之间和对象之间是否相等,相当于Object的equals方法 - 忽略大小写判断是否相等
assertThat( testedString, equalToIgnoringCase( “developerWorks” ) );
注释:equalToIgnoringCase匹配符表明如果测试的字符串testedString在忽略大小写的情况下等于”developerWorks”则测试通过 - 忽略头尾的任意个空格的情况下等于待测字符串
assertThat( testedString, equalToIgnoringWhiteSpace( “developerWorks” ) );
注释:equalToIgnoringWhiteSpace匹配符表明如果测试的字符串testedString在忽略头尾的任意个空格的情况下等于”developerWorks”则测试通过,注意:字符串中的空格不能被忽略
五、数值相关匹配符
- 范围测试
assertThat( testedDouble, closeTo( 20.0, 0.5 ) );
注释:closeTo匹配符表明如果所测试的浮点型数testedDouble在20.0±0.5范围之内则测试通过 - 大于判断
assertThat( testedNumber, greaterThan(16.0) );
注释:greaterThan匹配符表明如果所测试的数值testedNumber大于16.0则测试通过 - 小于判断
assertThat( testedNumber, lessThan (16.0) );
注释:lessThan匹配符表明如果所测试的数值testedNumber小于16.0则测试通过 - 大于等于
assertThat( testedNumber, greaterThanOrEqualTo (16.0) );
注释: greaterThanOrEqualTo匹配符表明如果所测试的数值testedNumber大于等于16.0则测试通过 - 小于等于
assertThat( testedNumber, lessThanOrEqualTo (16.0) );
注释:lessThanOrEqualTo匹配符表明如果所测试的数值testedNumber小于等于16.0则测试通过
六、collection相关匹配符
- map包含测试
assertThat( mapObject, hasEntry( “key”, “value” ) );
注释:hasEntry匹配符表明如果测试的Map对象mapObject含有一个键值为”key”对应元素值为”value”的Entry项则测试通过 - 迭代对象包含测试
assertThat( iterableObject, hasItem ( “element” ) );
注释:hasItem匹配符表明如果测试的迭代对象iterableObject含有元素“element”项则测试通过 - map包含key测试
assertThat( mapObject, hasKey ( “key” ) );
注释: hasKey匹配符表明如果测试的Map对象mapObject含有键值“key”则测试通过 - map包含value测试
assertThat( mapObject, hasValue ( “key” ) );
注释:hasValue匹配符表明如果测试的Map对象mapObject含有元素值“value”则测试通过
七、使用示例
示例采用spring3.0来做测试,junit使用junit4.4,IDE为IDEA2016.1
applicationContext.xml中的内容
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config></context:annotation-config> <bean name="user" class="com.frank.spring.model.User"> </bean> <bean name="frank" class="com.frank.spring.model.Frank"> </bean></beans>
User类的声明
package com.frank.spring.model;/** * Created by WHUER on 2016/5/24 0024. */public class User { private int id; private String name; private String sex; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public User() { this.setId(1); this.setName("frank"); this.setSex("male"); } @Override public String toString() { return "id:"+this.getId()+"\n" +"name:"+this.getName()+"\n" +"sex:"+this.getSex(); }}
Frank类声明
package com.frank.spring.model;import org.springframework.beans.factory.annotation.Autowired;/** * Created by WHUER on 2016/5/24 0024. */public class Frank { private User user; public User getUser() { return user; } @Autowired public void setUser(User user) { this.user = user; } //这里还是使用autowired,它也可以用于普通的方法之上 @Autowired public void initUser(User user) { this.setUser(user); } public String changeUserInfo() { user.setSex("男"); return user.getSex(); }}
JUnit测试类示例
package com.frank.spring.test;import com.frank.spring.model.Frank;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import static org.hamcrest.CoreMatchers.equalTo;import static org.junit.Assert.*;/** * Created by WHUER on 2016/5/24 0024. */public class FrankTest { ApplicationContext ctx; @org.junit.Before public void setUp() throws Exception { ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); } @org.junit.After public void tearDown() throws Exception { System.out.println("测试结束"); } /** * 这个注解采用的是AutoWired * @throws Exception */ @org.junit.Test public void getUser() throws Exception { Frank frank = (Frank) ctx.getBean("frank"); System.out.println(frank.getUser().toString()); } @Test public void changeUserInfo() throws Exception { Frank frank = (Frank) ctx.getBean("frank"); assertSame("测试通过","男",frank.changeUserInfo()); assertThat(frank.changeUserInfo(),equalTo("男")); }}
八、项目使用lib详情
commons-beanutils-1.8.0.jar commons-codec-1.9.jar commons-collections-3.1.jar commons-fileupload-1.3.1.jar commons-io-2.2.jar commons-lang3-3.2.jar commons-lang-2.4.jar commons-logging-1.1.3.jar commons-logging-api-1.1.jar commons-net-3.4.jar org.springframework.aop-3.0.1.RELEASE-A.jar org.springframework.asm-3.0.1.RELEASE-A.jar org.springframework.aspects-3.0.1.RELEASE-A.jar org.springframework.beans-3.0.1.RELEASE-A.jar org.springframework.context.support-3.0.1.RELEASE-A.jar org.springframework.context-3.0.1.RELEASE-A.jar org.springframework.core-3.0.1.RELEASE-A.jar org.springframework.expression-3.0.1.RELEASE-A.jar org.springframework.instrument.tomcat-3.0.1.RELEASE-A.jar org.springframework.instrument-3.0.1.RELEASE-A.jar org.springframework.jdbc-3.0.1.RELEASE-A.jar org.springframework.jms-3.0.1.RELEASE-A.jar org.springframework.orm-3.0.1.RELEASE-A.jar org.springframework.oxm-3.0.1.RELEASE-A.jar org.springframework.test-3.0.1.RELEASE-A.jar org.springframework.transaction-3.0.1.RELEASE-A.jar org.springframework.web.portlet-3.0.1.RELEASE-A.jar org.springframework.web.servlet-3.0.1.RELEASE-A.jar org.springframework.web.struts-3.0.1.RELEASE-A.jar org.springframework.web-3.0.1.RELEASE-A.jar
九、运行结果
根据上图的结果,我们可以看到green ,证明junit测试通过了
0 0
- JUnit之断言assert
- Junit中的断言(Assert)
- Junit-断言(Assert)使用方法
- java之断言assert
- java之断言assert
- junit测试assert各种断言用法
- Junit之断言测试
- junit之断言
- Java 之 assert (断言)
- C++技巧之断言Assert
- C++技巧之断言Assert
- C++技巧之断言Assert
- C++技巧之断言Assert
- 使用junit Assert 断言来判断参数是否为空
- JUnit 测试-Assert断言相关方法简析
- junit之Matcher,Assert.assertThat
- 断言assert
- 断言 ASSERT
- nginx 根据IP 进行灰度发布
- Deep Neural Networks are Easily Fooled:High Confidence Predictions for Unrecognizable Images
- 设计模式之六建造者模式
- 下载SAPUI5 SDK
- 字符串的反转和其它类型转换成字符串
- JUnit之断言assert
- 音频audio
- Spark词频统计测试
- html 基础
- BZOJ3028 食物
- (4.6.12)AndroidManifest.xml常用属性大全[进行中]
- 【codeforces】-#610B-Vika and Squares(一串数循环一圈后递减,遇0结束,求最大)
- 多线程(4)单列模式
- CentOS Linux服务器安全设置