博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Assertions
阅读量:5285 次
发布时间:2019-06-14

本文共 8176 字,大约阅读时间需要 27 分钟。

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

特别要提到的一种断言是assertThat,它的参数是([失败时打印的字符串消息],实际值,Matcher对象),参数顺序和其他的断言方法正好相反。同时由于生成Matcher对象需要用到”org.hamcrest.CoreMatchers.*"里面的方法,所以使用assertThat的时候需要额外导入hamcrest-core.jar和hamcrest-library.jar (下载地址:)。关于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官网:。

Hamcrest主页:

Hamcrest整个包在GitHub的下载地址:

Method Summary
static void (boolean[] expecteds, boolean[] actuals)
          Asserts that two boolean arrays are equal.
static void (byte[] expecteds, byte[] actuals)
          Asserts that two byte arrays are equal.
static void (char[] expecteds, char[] actuals)
          Asserts that two char arrays are equal.
static void (double[] expecteds, double[] actuals, double delta)
          Asserts that two double arrays are equal.
static void (float[] expecteds, float[] actuals, float delta)
          Asserts that two float arrays are equal.
static void (int[] expecteds, int[] actuals)
          Asserts that two int arrays are equal.
static void (long[] expecteds, long[] actuals)
          Asserts that two long arrays are equal.
static void ([] expecteds,[] actuals)
          Asserts that two object arrays are equal.
static void (short[] expecteds, short[] actuals)
          Asserts that two short arrays are equal.
static void ( message, boolean[] expecteds, boolean[] actuals)
          Asserts that two boolean arrays are equal.
static void ( message, byte[] expecteds, byte[] actuals)
          Asserts that two byte arrays are equal.
static void ( message, char[] expecteds, char[] actuals)
          Asserts that two char arrays are equal.
static void ( message, double[] expecteds, double[] actuals, double delta)
          Asserts that two double arrays are equal.
static void ( message, float[] expecteds, float[] actuals, float delta)
          Asserts that two float arrays are equal.
static void ( message, int[] expecteds, int[] actuals)
          Asserts that two int arrays are equal.
static void ( message, long[] expecteds, long[] actuals)
          Asserts that two long arrays are equal.
static void ( message,[] expecteds,[] actuals)
          Asserts that two object arrays are equal.
static void ( message, short[] expecteds, short[] actuals)
          Asserts that two short arrays are equal.
static void (double expected, double actual)
          Deprecated. Use assertEquals(double expected, double actual, double delta) instead
static void (double expected, double actual, double delta)
          Asserts that two doubles are equal to within a positive delta.
static void (float expected, float actual, float delta)
          Asserts that two floats are equal to within a positive delta.
static void (long expected, long actual)
          Asserts that two longs are equal.
static void ([] expecteds,[] actuals)
          Deprecated. use assertArrayEquals
static void ( expected, actual)
          Asserts that two objects are equal.
static void ( message, double expected, double actual)
          Deprecated. Use assertEquals(String message, double expected, double actual, double delta) instead
static void ( message, double expected, double actual, double delta)
          Asserts that two doubles are equal to within a positive delta.
static void ( message, float expected, float actual, float delta)
          Asserts that two floats are equal to within a positive delta.
static void ( message, long expected, long actual)
          Asserts that two longs are equal.
static void ( message,[] expecteds,[] actuals)
          Deprecated. use assertArrayEquals
static void ( message, expected, actual)
          Asserts that two objects are equal.
static void (boolean condition)
          Asserts that a condition is false.
static void ( message, boolean condition)
          Asserts that a condition is false.
static void (double unexpected, double actual, double delta)
          Asserts that two doubles are not equal to within a positive delta.
static void (float unexpected, float actual, float delta)
          Asserts that two floats are not equal to within a positive delta.
static void (long unexpected, long actual)
          Asserts that two longs are not equals.
static void ( unexpected, actual)
          Asserts that two objects are not equals.
static void ( message, double unexpected, double actual, double delta)
          Asserts that two doubles are not equal to within a positive delta.
static void ( message, float unexpected, float actual, float delta)
          Asserts that two floats are not equal to within a positive delta.
static void ( message, long unexpected, long actual)
          Asserts that two longs are not equals.
static void ( message, unexpected, actual)
          Asserts that two objects are not equals.
static void ( object)
          Asserts that an object isn't null.
static void ( message, object)
          Asserts that an object isn't null.
static void ( unexpected, actual)
          Asserts that two objects do not refer to the same object.
static void ( message, unexpected, actual)
          Asserts that two objects do not refer to the same object.
static void ( object)
          Asserts that an object is null.
static void ( message, object)
          Asserts that an object is null.
static void ( expected, actual)
          Asserts that two objects refer to the same object.
static void ( message, expected, actual)
          Asserts that two objects refer to the same object.
static
<T> void
( reason, T actual, <? super T> matcher)
          Asserts that actual satisfies the condition specified by matcher.
static
<T> void
(T actual,<? super T> matcher)
          Asserts that actual satisfies the condition specified by matcher.
static void (boolean condition)
          Asserts that a condition is true.
static void ( message, boolean condition)
          Asserts that a condition is true.
static void ()
          Fails a test with no message.
static void ( 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.
either(equalTo(3)).or(equalTo(4)))); assertThat(new Object(), not(sameInstance(new Object()))); } @Test public void testAssertTrue() { assertTrue("failure - should be true", true); } }

 

转载于:https://www.cnblogs.com/miniren/p/4638520.html

你可能感兴趣的文章
NOIP2016提高A组五校联考2总结
查看>>
iOS 项目的编译速度提高
查看>>
table中checkbox选择多行
查看>>
Magento开发文档(三):Magento控制器
查看>>
性能调优攻略
查看>>
ie6解决png图片透明问题
查看>>
瞬间的永恒
查看>>
2019-8-5 考试总结
查看>>
JS中实现字符串和数组的相互转化
查看>>
web service和ejb的区别
查看>>
Windows Azure Cloud Service (29) 在Windows Azure发送邮件(下)
查看>>
微信上传素材返回 '{"errcode":41005,"errmsg":"media data missing"}',php5.6返回
查看>>
div或者p标签单行和多行超出显示省略号
查看>>
Elasticsearch 滚动重启 必读
查看>>
Hadoop基本概念
查看>>
java.util.zip压缩打包文件总结一:压缩文件及文件下面的文件夹
查看>>
浅说 apache setenvif_module模块
查看>>
MySQL--数据插入
查看>>
重新学习python系列(二)? WTF?
查看>>
shell脚本统计文件中单词的个数
查看>>