Assertions
来源:互联网 发布:lte无线网络优化 编辑:程序博客网 时间:2024/05/01 15:07
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
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)
insteadstatic 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)
insteadstatic 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)
官网的断言示例如下:
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 {@Testpublic void testAssertArrayEquals(){byte[] expected="trial".getBytes();byte[] actual="trial".getBytes();assertArrayEquals("failure-byte arrays not same",expected, actual);}@Testpublic void testAssertEquals(){assertEquals("failure-strings are not equal","test","test");}@Testpublic void testAssertFalse(){assertFalse("failure-should be false",false);}@Testpublic void testAssertNotNull(){assertNotNull("should not be null",new Object());}@Testpublic void testAssertNotSame(){assertNotSame("should not be same object",new Object(),new String("hello"));}@Testpublic void testAssertNull(){assertNull("should be null",null);}@Testpublic 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); }}
0 0
- Assertions
- Assertions
- Enhanced Assertions
- Java: Assertions
- Assertions#assert_in_delta
- Assertions#assert_equal
- JMeter Assertions
- 编译期间Assertions
- assertions 使用问答
- JUnit Assertions vs assert
- Junit5断言(Assertions)
- Junit (Assertions in JUNIT)
- why use assertions in code
- Enhanced Assertions一文笔记
- 第九节swift 断言(Assertions)
- SVA(system verilog assertions)基础
- [1]Junit4-Assertions的使用
- 2.Flow Control, Assertions And Exception Handing
- 批量导出informatica object 的shell script
- 循环队列演示
- 一个调整应用图标大小的小工具
- Cocos2d-x 3.0坐标系详解
- 力所能及之struts2拦截器
- Assertions
- LDM522-MINI射频读卡模块 迷你款 手持平板门禁首选
- QT串口编程的相关类(QSerialPortInfo)
- C编译器剖析_3.3 语法分析_C语言的外部声明(1)
- s:iterator遍历
- 八皇后问题
- 小结:9 跳转指令小结
- vs2012安装失败
- make常见错误