Assert测试类
- 介绍以下Junit的官方文档:Junit
- 看一下测试类里的断言方法
代码如下:
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", "text", "text");
}
@Test
public void testAssertFalse() {
//测试布尔值输出是否为false
assertFalse("failure - should be false", false);
}
@Test
public void testAssertNotNull() {
//测试对象输出是否为空
assertNotNull("should not be null", new Object());
}
@Test
public void testAssertNotSame() {
//测试对象A与对象B输出不同
assertNotSame("should not be same Object", new Object(), new Object());
}
@Test
public void testAssertNull() {
//测试对象输出为空
assertNull("should be null", null);
}
@Test
public void testAssertSame() {
Integer aNumber = Integer.valueOf(768);
//测试对象A与对象B输出相同
assertSame("should be same", aNumber, aNumber);
}
// JUnit Matchers assertThat
@Test
public void testAssertThatBothContainsString() {
assertThat("albumen", both(containsString("a")).and(containsString("b")));
}
@Test
public void testAssertThatHasItems() {
assertThat(Arrays.asList("one", "two", "three"), hasItems("one", "three"));
}
@Test
public void testAssertThatEveryItemContainsString() {
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);
}
}