JUnit 断言

– Start
下面的例子演示了各种方式的断言。

package demo06;

import java.util.ArrayList;
import java.util.List;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

class AssertionTest {

	@Test
	void test() {
		// 断言 null
		Assertions.assertNotNull("");
		Assertions.assertNull(null);

		// 断言相等
		Assertions.assertEquals(1, 1);
		Assertions.assertNotEquals(1, 2);

		// 断言 true 和 false
		Assertions.assertTrue(true);
		Assertions.assertFalse(false);

		// 断言相同,用 == 判断
		Assertions.assertNotSame(1, 2);
		Assertions.assertSame(1, 1);

		// 批量断言
		Assertions.assertAll(
				() -> Assertions.assertEquals(1, 1), 
				() -> Assertions.assertEquals(2, 2));

		// 断言数组相等
		Assertions.assertArrayEquals(new String[] { "a" }, new String[] { "a" });

		// 断言可迭代对象相等
		List l = new ArrayList<>();
		Assertions.assertIterableEquals(l, l);
		Assertions.assertLinesMatch(l, l);
	}

}

– 更多参见:JUnit 精萃
– 声 明:转载请注明出处
– Last Updated on 2019-08-12
– Written by ShangBo on 2019-08-12
– End

你可能感兴趣的:(JUnit)