Assertions
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, Asserts that two boolean arrays are equal. |
|
static void |
assertArrayEquals(String message, Asserts that two byte arrays are equal. |
|
static void |
assertArrayEquals(String message, Asserts that two char arrays are equal. |
|
static void |
assertArrayEquals(String message, Asserts that two double arrays are equal. |
|
static void |
assertArrayEquals(String message, Asserts that two float arrays are equal. |
|
static void |
assertArrayEquals(String message, Asserts that two int arrays are equal. |
|
static void |
assertArrayEquals(String message, 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, 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, Deprecated. Use assertEquals(String message, double expected, double actual, double delta) instead |
|
static void |
assertEquals(String message, Asserts that two doubles are equal to within a positive delta. |
|
static void |
assertEquals(String message, Asserts that two floats are equal to within a positive delta. |
|
static void |
assertEquals(String message, 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, 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, Asserts that two doubles are not equal to within a positive delta. |
|
static void |
assertNotEquals(String message, Asserts that two floats are not equal to within a positive delta. |
|
static void |
assertNotEquals(String message, 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
|
assertThat(String reason, Asserts that actual satisfies the condition specified by . |
|
static
|
assertThat(T actual,Matcher<? Asserts that actual satisfies the condition specified by . |
|
static void |
assertTrue(boolean condition)Asserts that a condition is true. |
|
static void |
assertTrue(String message, Asserts that a condition is true. |
|
static void |
fail()Fails a test with no message. |
|
static void |
fail(String message) |
|
官网的断言示例如下:
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*; import java.util.Arrays;
import org.hamcrest.core.CombinableMatcher;
import org.junit.Test; public class AssertTests { @Test
public void testAssertArrayEquals(){
byte[] expected="trial".getBytes();
byte[] actual="trial".getBytes();
assertArrayEquals("failure-byte arrays not same",expected, actual);
} @Test
public void testAssertEquals(){
assertEquals("failure-strings are not equal","test","test");
} @Test
public void testAssertFalse(){
assertFalse("failure-should be false",false);
} @Test
public void testAssertNotNull(){
assertNotNull("should not be null",new Object());
} @Test
public void testAssertNotSame(){
assertNotSame("should not be same object",new Object(),new String("hello"));
} @Test
public void testAssertNull(){
assertNull("should be null",null);
} @Test
public void testAssertSame(){
Integer aNumber=Integer.valueOf(78);
assertSame("should be the same",aNumber,aNumber);
} // JUnit Matchers assertThat
@Test
public void testAssertThatBothContainsString() {
org.junit.Assert.assertThat("albumen", both(containsString("a")).and(containsString("b")));
} @Test
public void testAssertThathasItemsContainsString() {
org.junit.Assert.assertThat(Arrays.asList("one", "two", "three"), hasItems("one", "three"));
} @Test
public void testAssertThatEveryItemContainsString() {
org.junit.Assert.assertThat(Arrays.asList(new String[] { "fun", "ban", "net" }), everyItem(containsString("n")));
} // Core Hamcrest Matchers with assertThat
@Test
public void testAssertThatHamcrestCoreMatchers() {
assertThat("good", allOf(equalTo("good"), startsWith("good")));
assertThat("good", not(allOf(equalTo("bad"), equalTo("good"))));
assertThat("good", anyOf(equalTo("bad"), equalTo("good")));
assertThat(7, not(CombinableMatcher.<Integer> either(equalTo(3)).or(equalTo(4))));
assertThat(new Object(), not(sameInstance(new Object())));
} @Test
public void testAssertTrue() {
assertTrue("failure - should be true", true);
}
}
Assertions的更多相关文章
- SVA(system verilog assertions)基础
1什么是断言: 断言就是在模拟过程中依据我们事先安排好的逻辑是不是发生了,假设发生断言成功.否则断言失败. 2断言的运行分为:预备(preponed)观察(observed)响应(reactive). ...
- 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 ...
- 正则表达式lookahead and lookbehind zero-length assertions
正则表达式非常好的网站: https://www.regular-expressions.info/lookaround.html ---------------------------------- ...
- Swift学习笔记(2)--元组(Tuples)、Optional(可选值)、(Assertions)断言
1.Tuples(元组) 元组是多个值组合而成的复合值.元组中的值可以是任意类型,而且每一个元素的类型可以是不同的. 1>定义:使用()包含所有元素,用逗号分开,也可以对每个元素做命名 let ...
- 『BASH』——Hadex's brief analysis of "Lookahead and Lookbehind Zero-Length Assertions"
/*为节省时间,本文以汉文撰写*/ -前言- 深入学习正则表达式,可以很好的提高思维逻辑的缜密性:又因正则应用于几乎所有高级编程语言,其重要性不言而喻,是江湖人士必备的内功心法. 正则表达式概要(ob ...
- .NET Core之单元测试(四):Fluent Assertions的使用
目录 什么是Fluent Assertions 待测试API 测试用例 什么是Fluent Assertions Fluent Assertions 是 .NET 平台下的一组扩展方法,用于单元测试中 ...
- JUnit5学习之三:Assertions类
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- Junit 的Assertions的使用
import static org.hamcrest.CoreMatchers.allOf; import static org.hamcrest.CoreMatchers.anyOf; import ...
- [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 ...
随机推荐
- 移动端网站或APP点击后出现闪动或灰色背景
隐藏文本框阴影 input, textarea{-webkit-appearance: @none;} 取消手机点击屏幕时,会出现的灰块 html,body{-webkit-text-size-adj ...
- android studio主题设置-笔记3
主题背景设置(就是工具黑色背景还是白色背景),路径:File-Settings-Appearance
- 【转】 iOS使用AVFoundation实现二维码扫描
原文:http://strivingboy.github.io/blog/2014/11/08/scan-qrcode/ 关于二维码扫描有不少优秀第三方库如: ZBar SDK 里面有详细的文档,相应 ...
- statistic学习笔记
1. 假设检验:就是对于符合一定前提条件的数据,先作一个假设H0,还有一个备择假设H1(一般是H0的反面,或者是H0不包含的情况),通过一定的计算公式,算出一个值(比如开方检验就是开方值),这个值的理 ...
- ONVIF Event消息解析(How to work with gSoap)
Prepare Requirements ONVIF Event gSoap reference ONVIF Specification 问题描述 Event是ONVIF核心规范中一块, 文档解释了如 ...
- PHP Predefined Interfaces 预定义接口
SPL提供了6个迭代器接口: Traversable 遍历接口(检测一个类是否可以使用 foreach 进行遍历的接口) Iterator 迭代器接口(可在内部迭代自己的外部迭代器或类的接口) Ite ...
- Firefox中Vimperator插件配置
具体配置什么,同学们可以网上看下善用佳软关于Vimperator的说明,在这里我列出两条我个人觉得最有用的命令 set nextpattern=\s*下一页|下一张|下一篇|下页|后页\s*,^\bn ...
- 小议window.event || ev
以前做项目时就遇到这个问题,但是太懒没有总结,今天来总结一下 onclick="alert(arguments.callee)"这句随便放在某个元素中,试试不同的浏览器会有弹出什么 ...
- extjs之TypeError: d.read is not a function解决方案
在创建如下代码时报出此错:TypeError: d.read is not a function Ext.define('shebyxgl_sheb_model', { extend: 'Ext.da ...
- 如何去除 ckeditor 上传图片后在原码中留下的 style="width: 100%;height:100px"之类的代码呢?
ckeditor编辑器在上传图片的时候,会神奇的加上一段诡异的代码: 这导致上传的小图也是被拉伸到100%,我根本就没有定义它,找来找去也找不到element.style,原来这是在system.cs ...