JUnit提供了许多重载的断言方法,这些方法均可以通过"import static org.junit.Assert.*"导入。方法的参数顺序一般都是([失败时打印的字符串消息],期望值,实际值)。

特别要提到的一种断言是assertThat,它的参数是([失败时打印的字符串消息],实际值,Matcher对象),参数顺序和其他的断言方法正好相反。同时由于生成Matcher对象需要用到”org.hamcrest.CoreMatchers.*"里面的方法,所以使用assertThat的时候需要额外导入hamcrest-core.jar和hamcrest-library.jar (下载地址:http://search.maven.org/#search|ga|1|g%3Aorg.hamcrest)。关于AssertThat的详细介绍参见 ”Matchers and assertthat"。

Hamcrest是什么呢?它的官网是这样说的:amcrest is a library of matchers, which can be combined in to create flexible expressions of intent in tests。按我的理解就是Hamcrest是一个Library,它提供了一套匹配符Matcher,这些匹配符可读性高而且灵活,所以JUnit4引入了Hamcrest框架。

Hamcrest官网:http://hamcrest.org/

Hamcrest主页:http://code.google.com/p/hamcrest/wiki/Tutorial

Hamcrest整个包在GitHub的下载地址:https://github.com/hamcrest/JavaHamcrest

Method Summary
static void assertArrayEquals(boolean[] expecteds, boolean[] actuals)
          Asserts that two boolean arrays are equal.
static void assertArrayEquals(byte[] expecteds, byte[] actuals)
          Asserts that two byte arrays are equal.
static void assertArrayEquals(char[] expecteds, char[] actuals)
          Asserts that two char arrays are equal.
static void assertArrayEquals(double[] expecteds, double[] actuals, double delta)
          Asserts that two double arrays are equal.
static void assertArrayEquals(float[] expecteds, float[] actuals, float delta)
          Asserts that two float arrays are equal.
static void assertArrayEquals(int[] expecteds, int[] actuals)
          Asserts that two int arrays are equal.
static void assertArrayEquals(long[] expecteds, long[] actuals)
          Asserts that two long arrays are equal.
static void assertArrayEquals(Object[] expecteds,Object[] actuals)
          Asserts that two object arrays are equal.
static void assertArrayEquals(short[] expecteds, short[] actuals)
          Asserts that two short arrays are equal.
static void assertArrayEquals(String message,
boolean[] expecteds, boolean[] actuals)

          Asserts that two boolean arrays are equal.
static void assertArrayEquals(String message,
byte[] expecteds, byte[] actuals)

          Asserts that two byte arrays are equal.
static void assertArrayEquals(String message,
char[] expecteds, char[] actuals)

          Asserts that two char arrays are equal.
static void assertArrayEquals(String message,
double[] expecteds, double[] actuals, double delta)

          Asserts that two double arrays are equal.
static void assertArrayEquals(String message,
float[] expecteds, float[] actuals, float delta)

          Asserts that two float arrays are equal.
static void assertArrayEquals(String message,
int[] expecteds, int[] actuals)

          Asserts that two int arrays are equal.
static void assertArrayEquals(String message,
long[] expecteds, long[] actuals)

          Asserts that two long arrays are equal.
static void assertArrayEquals(String message,Object[] expecteds,Object[] actuals)
          Asserts that two object arrays are equal.
static void assertArrayEquals(String message,
short[] expecteds, short[] actuals)

          Asserts that two short arrays are equal.
static void assertEquals(double expected, double actual)
          Deprecated. Use assertEquals(double expected, double actual, double delta) instead
static void assertEquals(double expected, double actual, double delta)
          Asserts that two doubles are equal to within a positive delta.
static void assertEquals(float expected, float actual, float delta)
          Asserts that two floats are equal to within a positive delta.
static void assertEquals(long expected, long actual)
          Asserts that two longs are equal.
static void assertEquals(Object[] expecteds,Object[] actuals)
          Deprecated. use assertArrayEquals
static void assertEquals(Object expected,Object actual)
          Asserts that two objects are equal.
static void assertEquals(String message,
double expected, double actual)

          Deprecated. Use assertEquals(String message, double expected, double actual, double delta) instead
static void assertEquals(String message,
double expected, double actual, double delta)

          Asserts that two doubles are equal to within a positive delta.
static void assertEquals(String message,
float expected, float actual, float delta)

          Asserts that two floats are equal to within a positive delta.
static void assertEquals(String message,
long expected, long actual)

          Asserts that two longs are equal.
static void assertEquals(String message,Object[] expecteds,Object[] actuals)
          Deprecated. use assertArrayEquals
static void assertEquals(String message,Object expected,Object actual)
          Asserts that two objects are equal.
static void assertFalse(boolean condition)
          Asserts that a condition is false.
static void assertFalse(String message,
boolean condition)

          Asserts that a condition is false.
static void assertNotEquals(double unexpected, double actual, double delta)
          Asserts that two doubles are not equal to within a positive delta.
static void assertNotEquals(float unexpected, float actual, float delta)
          Asserts that two floats are not equal to within a positive delta.
static void assertNotEquals(long unexpected, long actual)
          Asserts that two longs are not equals.
static void assertNotEquals(Object unexpected,Object actual)
          Asserts that two objects are not equals.
static void assertNotEquals(String message,
double unexpected, double actual, double delta)

          Asserts that two doubles are not equal to within a positive delta.
static void assertNotEquals(String message,
float unexpected, float actual, float delta)

          Asserts that two floats are not equal to within a positive delta.
static void assertNotEquals(String message,
long unexpected, long actual)

          Asserts that two longs are not equals.
static void assertNotEquals(String message,Object unexpected,Object actual)
          Asserts that two objects are not equals.
static void assertNotNull(Object object)
          Asserts that an object isn't null.
static void assertNotNull(String message,Object object)
          Asserts that an object isn't null.
static void assertNotSame(Object unexpected,Object actual)
          Asserts that two objects do not refer to the same object.
static void assertNotSame(String message,Object unexpected,Object actual)
          Asserts that two objects do not refer to the same object.
static void assertNull(Object object)
          Asserts that an object is null.
static void assertNull(String message,Object object)
          Asserts that an object is null.
static void assertSame(Object expected,Object actual)
          Asserts that two objects refer to the same object.
static void assertSame(String message,Object expected,Object actual)
          Asserts that two objects refer to the same object.
static

<T> void
assertThat(String reason,
T actual, Matcher<? super T> matcher)

          Asserts that actual satisfies the condition specified by
matcher
.
static

<T> void
assertThat(T actual,Matcher<?
super T> matcher)

          Asserts that actual satisfies the condition specified by
matcher
.
static void assertTrue(boolean condition)
          Asserts that a condition is true.
static void assertTrue(String message,
boolean condition)

          Asserts that a condition is true.
static void fail()
          Fails a test with no message.
static void fail(String message)

官网的断言示例如下:

  1. import static org.junit.Assert.*;
  2. import static org.hamcrest.CoreMatchers.*;
  3.  
  4. import java.util.Arrays;
  5. import org.hamcrest.core.CombinableMatcher;
  6. import org.junit.Test;
  7.  
  8. public class AssertTests {
  9.  
  10. @Test
  11. public void testAssertArrayEquals(){
  12. byte[] expected="trial".getBytes();
  13. byte[] actual="trial".getBytes();
  14. assertArrayEquals("failure-byte arrays not same",expected, actual);
  15. }
  16.  
  17. @Test
  18. public void testAssertEquals(){
  19. assertEquals("failure-strings are not equal","test","test");
  20. }
  21.  
  22. @Test
  23. public void testAssertFalse(){
  24. assertFalse("failure-should be false",false);
  25. }
  26.  
  27. @Test
  28. public void testAssertNotNull(){
  29. assertNotNull("should not be null",new Object());
  30. }
  31.  
  32. @Test
  33. public void testAssertNotSame(){
  34. assertNotSame("should not be same object",new Object(),new String("hello"));
  35. }
  36.  
  37. @Test
  38. public void testAssertNull(){
  39. assertNull("should be null",null);
  40. }
  41.  
  42. @Test
  43. public void testAssertSame(){
  44. Integer aNumber=Integer.valueOf(78);
  45. assertSame("should be the same",aNumber,aNumber);
  46. }
  47.  
  48. // JUnit Matchers assertThat
  49. @Test
  50. public void testAssertThatBothContainsString() {
  51. org.junit.Assert.assertThat("albumen", both(containsString("a")).and(containsString("b")));
  52. }
  53.  
  54. @Test
  55. public void testAssertThathasItemsContainsString() {
  56. org.junit.Assert.assertThat(Arrays.asList("one", "two", "three"), hasItems("one", "three"));
  57. }
  58.  
  59. @Test
  60. public void testAssertThatEveryItemContainsString() {
  61. org.junit.Assert.assertThat(Arrays.asList(new String[] { "fun", "ban", "net" }), everyItem(containsString("n")));
  62. }
  63.  
  64. // Core Hamcrest Matchers with assertThat
  65. @Test
  66. public void testAssertThatHamcrestCoreMatchers() {
  67. assertThat("good", allOf(equalTo("good"), startsWith("good")));
  68. assertThat("good", not(allOf(equalTo("bad"), equalTo("good"))));
  69. assertThat("good", anyOf(equalTo("bad"), equalTo("good")));
  70. assertThat(7, not(CombinableMatcher.<Integer> either(equalTo(3)).or(equalTo(4))));
  71. assertThat(new Object(), not(sameInstance(new Object())));
  72. }
  73.  
  74. @Test
  75. public void testAssertTrue() {
  76. assertTrue("failure - should be true", true);
  77. }
  78. }

Assertions的更多相关文章

  1. SVA(system verilog assertions)基础

    1什么是断言: 断言就是在模拟过程中依据我们事先安排好的逻辑是不是发生了,假设发生断言成功.否则断言失败. 2断言的运行分为:预备(preponed)观察(observed)响应(reactive). ...

  2. How to view assertions in the Verdi waveform viewer

    In the Cadence Simvision waveform viewer, I can see every assertions listed as a hierarchical signal ...

  3. 正则表达式lookahead and lookbehind zero-length assertions

    正则表达式非常好的网站: https://www.regular-expressions.info/lookaround.html ---------------------------------- ...

  4. Swift学习笔记(2)--元组(Tuples)、Optional(可选值)、(Assertions)断言

    1.Tuples(元组) 元组是多个值组合而成的复合值.元组中的值可以是任意类型,而且每一个元素的类型可以是不同的. 1>定义:使用()包含所有元素,用逗号分开,也可以对每个元素做命名 let ...

  5. 『BASH』——Hadex's brief analysis of "Lookahead and Lookbehind Zero-Length Assertions"

    /*为节省时间,本文以汉文撰写*/ -前言- 深入学习正则表达式,可以很好的提高思维逻辑的缜密性:又因正则应用于几乎所有高级编程语言,其重要性不言而喻,是江湖人士必备的内功心法. 正则表达式概要(ob ...

  6. .NET Core之单元测试(四):Fluent Assertions的使用

    目录 什么是Fluent Assertions 待测试API 测试用例 什么是Fluent Assertions Fluent Assertions 是 .NET 平台下的一组扩展方法,用于单元测试中 ...

  7. JUnit5学习之三:Assertions类

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  8. Junit 的Assertions的使用

    import static org.hamcrest.CoreMatchers.allOf; import static org.hamcrest.CoreMatchers.anyOf; import ...

  9. [TypeScript] Work with DOM Elements in TypeScript using Type Assertions

    The DOM can be a bit tricky when it comes to typing. You never really know exactly what you're going ...

随机推荐

  1. Git和Github的应用与命令方法总结

    title: Git和Github的应用与命令方法总结 date: 2016-07-11 14:03:09 tags: git/github [本文摘抄自微信公众平台:AndroidDeveloper ...

  2. python摇骰子猜大小的小游戏

    #小游戏,摇筛子押大小的小游戏玩家初始有1000块钱,可以压大压小作为赌注 import random #定义摇筛子的函数: def roll_dice(number = 3,points = Non ...

  3. Android定时器功能实现方法

    在Android开发中,定时器一般有以下3种实现方法: 1.采用Handler与线程的sleep(long)方法 2.采用Handler的postDelayed(Runnable, long)方法 3 ...

  4. VB 活动添加item元素

    'ListView1.Columns.Clear() 'ListView1.Columns.Add("", 0, HorizontalAlignment.Center) 'List ...

  5. loop_login.sh

    [oracle@ora10g scripts]$ cat loop_login.sh #/bin/bash####################export ORACLE_BASE=/u01/app ...

  6. c#读取通达信历史数据的方法

    public Bar ReadBarMin(BinaryReader br, int instrumentId, long size) { int date = br.ReadUInt16(); in ...

  7. 跟我学android-使用Eclipse开发第一个Android应用(三)

    打开Eclipse,选择 File—New –Android Application Project Application Name  就是我们的 应用名称,也是我们在手机应用程序列表里看到的名称. ...

  8. 【USACO 1.2.1】挤牛奶

    [问题描述] 三个农民每天清晨5点起床,然后去牛棚给3头牛挤奶.第一个农民在300时刻(从5点开始计时,秒为单位)给他的牛挤奶,一直到1000时刻.第二个农民在700时刻开始,在 1200时刻结束.第 ...

  9. margin-top在IE与其他浏览器下的不同

    两个box,box1嵌套box2, box2使用margin-top在IE下与其他浏览器不同.待整理

  10. Asp.net的IP地址屏蔽功能设计

    "IP地址的长度为32位,分为4段,每段8位,用十进制数字表示,每段数字范围为0~255,段与段之间用句点隔开." 由此我们了解到,IP地址实际上是一个32位正整数,在C#中可以使 ...