JUnit之断言assert

一、简介

  1. JUnit4.4引入了Hamcrest框架,Hamcest提供了一套匹配符Matcher,这些匹配符更接近自然语言,可读性高,更加灵活;
  2. 使用全新的断言语法: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类的类图结构

assertl类的结构图

四、一般匹配方法

  1. 要求所有的条件都要通过测试才算成功
    assertThat( testedNumber, allOf( greaterThan(8), lessThan(16) ) );
    配符表明如果接下来的所有条件必须都成立测试才通过,相当于“与”(&&)
  2. 接下来的所有条件只要有一个成立则测试通过
    assertThat( testedNumber, anyOf( greaterThan(16), lessThan(8) ) );
    注释:anyOf匹配符表明如果接下来的所有条件只要有一个成立则测试通过,相当于“或”(||)
  3. 无论什么条件,永远为true
    assertThat( testedNumber, anything() );
    注释:anything匹配符表明无论什么条件,永远为true
  4. 等于判断
    assertThat( testedString, is( “developerWorks” ) );
    注释: is匹配符表明如果前面待测的object等于后面给出的object,则测试通过
  5. 取反判断
    assertThat( testedString, not( “developerWorks” ) );
    注释:not匹配符和is匹配符正好相反,表明如果前面待测的object不等于后面给出的object,则测试通过

四、字符串相关匹配符

  1. 包含字符串
    assertThat( testedString, containsString( “developerWorks” ) );
    注释:containsString匹配符表明如果测试的字符串testedString包含子字符串”developerWorks”则测试通过
  2. 以指定字符串结尾
    assertThat( testedString, endsWith( “developerWorks” ) );
    注释:endsWith匹配符表明如果测试的字符串testedString以子字符串”developerWorks”结尾则测试通过
  3. 以指定字符串开始
    assertThat( testedString, startsWith( “developerWorks” ) );
    注释:startsWith匹配符表明如果测试的字符串testedString以子字符串”developerWorks”开始则测试通过
  4. 字符串相等测试
    assertThat( testedValue, equalTo( expectedValue ) );
    注释: equalTo匹配符表明如果测试的testedValue等于expectedValue则测试通过,equalTo可以测试数值之间,字符串之间和对象之间是否相等,相当于Object的equals方法
  5. 忽略大小写判断是否相等
    assertThat( testedString, equalToIgnoringCase( “developerWorks” ) );
    注释:equalToIgnoringCase匹配符表明如果测试的字符串testedString在忽略大小写的情况下等于”developerWorks”则测试通过
  6. 忽略头尾的任意个空格的情况下等于待测字符串
    assertThat( testedString, equalToIgnoringWhiteSpace( “developerWorks” ) );
    注释:equalToIgnoringWhiteSpace匹配符表明如果测试的字符串testedString在忽略头尾的任意个空格的情况下等于”developerWorks”则测试通过,注意:字符串中的空格不能被忽略

五、数值相关匹配符

  1. 范围测试
    assertThat( testedDouble, closeTo( 20.0, 0.5 ) );
    注释:closeTo匹配符表明如果所测试的浮点型数testedDouble在20.0±0.5范围之内则测试通过
  2. 大于判断
    assertThat( testedNumber, greaterThan(16.0) );
    注释:greaterThan匹配符表明如果所测试的数值testedNumber大于16.0则测试通过
  3. 小于判断
    assertThat( testedNumber, lessThan (16.0) );
    注释:lessThan匹配符表明如果所测试的数值testedNumber小于16.0则测试通过
  4. 大于等于
    assertThat( testedNumber, greaterThanOrEqualTo (16.0) );
    注释: greaterThanOrEqualTo匹配符表明如果所测试的数值testedNumber大于等于16.0则测试通过
  5. 小于等于
    assertThat( testedNumber, lessThanOrEqualTo (16.0) );
    注释:lessThanOrEqualTo匹配符表明如果所测试的数值testedNumber小于等于16.0则测试通过

六、collection相关匹配符

  1. map包含测试
    assertThat( mapObject, hasEntry( “key”, “value” ) );
    注释:hasEntry匹配符表明如果测试的Map对象mapObject含有一个键值为”key”对应元素值为”value”的Entry项则测试通过
  2. 迭代对象包含测试
    assertThat( iterableObject, hasItem ( “element” ) );
    注释:hasItem匹配符表明如果测试的迭代对象iterableObject含有元素“element”项则测试通过
  3. map包含key测试
    assertThat( mapObject, hasKey ( “key” ) );
    注释: hasKey匹配符表明如果测试的Map对象mapObject含有键值“key”则测试通过
  4. 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测试通过了

  • 1
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值