Junit 4 Tutorials(Junit 4 教程) 三、Junit4 断言方法

Junit 4 断言方法允许检查测试方法的期望结果值和真实返回值。Junit的org.junit.Assert类提供了各种断言方法来写junit测试。这些方法被用来检查方法的真实结果值和期望值。下列一些有用的断言方法列表:

Junit 4 Assert Methods
Method Description
assertNull(java.lang.Object object) 检查对象是否为空
assertNotNull(java.lang.Object object) 检查对象是否不为空
assertEquals(long expected, long actual) 检查long类型的值是否相等
assertEquals(double expected, double actual, double delta) 检查指定精度的double值是否相等
assertFalse(boolean condition) 检查条件是否为假
assertTrue(boolean condition) 检查条件是否为真
assertSame(java.lang.Object expected, java.lang.Object actual) 检查两个对象引用是否引用同一对象(即对象是否相等)
assertNotSame(java.lang.Object unexpected, java.lang.Object actual)
检查两个对象引用是否不引用统一对象(即对象不等)

Junit 4断言方法样例

AssertionsTest.java junit测试用例,显示各种断言方法:

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

/**
* @author javatutorials.co.in
*/
public class AssertionsTest {

    @Test
    public void testAssertNull() {
        String str = null;
        assertNull(str);
    }

    @Test
    public void testAssertNotNull() {
        String str = "hello Java!!";
        assertNotNull(str);
    }

    @Test
    public void testAssertEqualsLong() {
        long long1 = 2;
        long long2 = 2;
        assertEquals(long1, long2);
    }

    @Test
    public void testAssertEqualsDouble() {
        // test case is successfull as double1 and double 2
        // differ by 0.001 which is less than our specified delta
        double double1 = 1.236;
        double double2 = 1.237;
        double delta = 0.002;
        assertEquals(double1, double2, delta);
    }

    @Test
    public void testAssertTrue() {
        List<String> list = new ArrayList<String>();
        assertTrue(list.isEmpty());
    }

    @Test
    public void testAssertFalse() {
        List<String> list = new ArrayList<String>();
        list.add("hello");
        assertFalse(list.isEmpty());
    }

    @Test
    public void testAssertSame() {
        String str1 = "hello world!!";
        String str2 = "hello world!!";
        assertSame(str2, str1);
    }

    @Test
    public void testAssertNotSame() {
        String str1 = "hello world!!";
        String str3 = "hello Java!!";
        assertNotSame(str1, str3);
    }
}

样例输出

在eclipse Junit 窗口的输出如下:

源码下载

点击我下载源码

教程目录导航

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

本文出处为 http://blog.csdn.net/luanlouis,转载请注明出处,谢谢!

Junit 4 Tutorials(Junit 4 教程) 三、Junit4 断言方法,布布扣,bubuko.com

时间: 2024-08-26 00:06:23

Junit 4 Tutorials(Junit 4 教程) 三、Junit4 断言方法的相关文章

Junit 4 Tutorials(Junit 4 教程) 四、Junit4 参数化测试

Junit 4 参数化测试 允许通过变化范围的参数值来测试方法.参数擦测试可以通过以下简单的步骤实现: 对测试类添加注解 @RunWith(Parameterized.class) 将需要使用变化范围参数值测试的参数定义为私有变量 使用上一步骤声明的私有变量作为入参,创建构造函数 .创建一个使用@Parameters注解的公共静态方法,它将需要测试的各种变量值通过集合的形式返回. 使用定义的私有变量定义测试方法 Junit 4 参数化测试样例 EvenNumberChecker.java 校验输

Junit 4 Tutorials(Junit 4 教程)

本教程是比较全面的.较新的Junit 4 实用教程,译自:http://www.javatutorials.co.in/junit-4/,希望对大家有所帮助! 以下是本教程的内容设置,读者可以点击以下标题进行阅读: Junit测试框架介绍 Junit 测试框架是最流行的Java单元测试框架.Junit被用来开发Java类的单元测试. Junit Eclipse教程 介绍eclipse IDE 中junit的相关特性及其使用方法.在eclipse IDE中书写和运行单元测试代码的样例. Junit

Junit 4 Tutorials(Junit 4 教程) 一、Junit简介及Junit Eclipse 教程

Junit 测试框架简介 测试框架是最流行的Java单元测试框架.Junit被用来开发对Java类的单元测试.它就是一个类包,提供了各种方法来测试Java类中的方法(method). Junit 4 特性 简单的注解,提供书写Junit测试的基本特性 断言方法(Assert Methods)比较测试的方法执行结果值和期望值 @Ignore 注解,忽略测试方法或者测试类的执行 期望异常测试 超时测试 , 测试方法的执行时间 测试组件,一起运行一些测试类 参数化测试, 以不同的输入参数值测试方法 J

测试教程网.unittest教程.7. 各种断言方法

From: http://www.testclass.net/pyunit/assert/ 背景 unittest支持各种断言方法. 断言列表 官方文档 方法 检查点 assertEqual(a, b) a == b assertNotEqual(a, b) a != b assertTrue(x) bool(x) is True assertFalse(x) bool(x) is False assertIs(a, b) a is b assertIsNot(a, b) a is not b

BootStrap入门教程 (三)

上讲回顾:Bootstrap的基础CSS(Base CSS)提供了优雅,一致的多种基础Html页面要素,包括排版,表格,表单,按钮等,能够满足前端工程师的基本要素需求. Bootstrap作为完整的前端工具集,内建了大量的强大优雅可重用的组件,包括按钮(Button),导航(Navigation),标签(Labels),徽章(Badges),排版(Typography),缩略图( thumbnails),提醒(Alert),进度条(progress bar),杂项(Miscellaneous).

BootStrap入门教程 (三) :可重用组件(按钮,导航,标签,徽章,排版,缩略图,提醒,进度条,杂项)

上讲回顾:Bootstrap的基础CSS(Base CSS)提供了优雅,一致的多种基础Html页面要素,包括排版,表格,表单,按钮等,能够满足前端工程师的基本要素需求. Bootstrap作为完整的前端工具集,内建了大量的强大优雅可重用的组件,包括按钮(Button),导航(Navigation),标签(Labels),徽章(Badges),排版(Typography),缩略图( thumbnails),提醒(Alert),进度条(progress bar),杂项(Miscellaneous).

微信开放平台 公众号第三方平台开发 教程三 一键登录授权给第三方平台

原文:微信开放平台 公众号第三方平台开发 教程三 一键登录授权给第三方平台 教程导航: 微信开放平台 公众号第三方平台开发 教程一 平台介绍 微信开放平台 公众号第三方平台开发 教程二 创建公众号第三方平台 微信开放平台 公众号第三方平台开发 教程三 一键登录授权给第三方平台 微信开放平台 公众号第三方平台开发 教程四 代公众号调用接口的SDK和demo 公众号第三方平台的开放,是为了让公众号运营者,在面向垂直行业需求时,可以一键登录授权给第三方的公众号运营平台,通过第三方开发者提供的公众号第三

JUnit实战(1) - JUnit起步(Parameterized参数化测试)

创建Java Project项目,项目名称:ch01-jumpstart Calculator.java public class Calculator { public double add(double number1, double number2) { return number1 + number2; } } CalculatorTest.java import static org.junit.Assert.*; import junit.framework.Assert; impo

struts2 官方系列教程三:使用struts2 标签 tag

避免被爬,先贴上本帖地址:struts2 官方系列教程一:使用struts2 标签 tag http://www.cnblogs.com/linghaoxinpian/p/6901316.html 本教材假定你已完成了HelloWorld项目,你可以在 struts2 官方系列教程三:使用struts2 标签 tag 下载本章节的代码 在上一节教程中,我们在index.jsp中使用 url tag 创建了一个超链接hello.action 这节我们将探索struts2中其它tags Web应用程