前言
由于JUnit的Assert是公认的烂API,所以不推荐使用,目前推荐使用的是AssertJ。
AssertJ网站: http://joel-costigliola.github.io/assertj/
github上examples 地址 https://github.com/joel-costigliola/assertj-examples
附件中assertj-examples-octo-assertj-core-1.5.0是官方examples
整理一些常用的例子如下
对字符串断言
- @Test
- public void testString() {
- String str = null;
-
- assertThat(str).isNullOrEmpty();
-
- assertThat("").isEmpty();
-
- assertThat("Frodo").isEqualTo("Frodo").isEqualToIgnoringCase("frodo");
-
- assertThat("Frodo").startsWith("Fro").endsWith("do").hasSize(5);
-
- assertThat("Frodo").contains("rod").doesNotContain("fro");
-
- assertThat("Frodo").containsOnlyOnce("do");
-
- assertThat("Frodo").matches("..o.o").doesNotMatch(".*d");
- }
对数字断言
- @Test
- public void testNumber() {
- Integer num = null;
-
- assertThat(num).isNull();
-
- assertThat(42).isEqualTo(42);
-
- assertThat(42).isGreaterThan(38).isGreaterThanOrEqualTo(38);
-
- assertThat(42).isLessThan(58).isLessThanOrEqualTo(58);
-
- assertThat(0).isZero();
-
- assertThat(1).isPositive().isNotNegative();
-
- assertThat(-1).isNegative().isNotPositive();
- }
对日期断言
- @Test
- public void testDate() {
-
- assertThat(Dates.parse("2014-02-01")).isEqualTo("2014-02-01").isNotEqualTo("2014-01-01")
- .isAfter("2014-01-01").isBefore(parse("2014-03-01"));
-
- assertThat(DateTime.now().toDate()).isBeforeYear(2020).isAfterYear(2013);
-
- assertThat(parse("2014-02-01")).isBetween("2014-01-01", "2014-03-01").isNotBetween(
- parse("2014-02-02"), parse("2014-02-28"));
-
-
- Date d1 = new Date();
- Date d2 = new Date(d1.getTime() + 100);
- assertThat(d1).isCloseTo(d2, 100);
-
-
- Date date1 = Dates.parseDatetimeWithMs("2003-01-01T01:00:00.000");
- Date date2 = parseDatetimeWithMs("2003-01-01T01:00:00.555");
- Date date3 = parseDatetimeWithMs("2003-01-01T01:00:55.555");
- Date date4 = parseDatetimeWithMs("2003-01-01T01:55:55.555");
- Date date5 = parseDatetimeWithMs("2003-01-01T05:55:55.555");
-
-
- assertThat(date1).isEqualToIgnoringMillis(date2);
-
- assertThat(date1).isInSameSecondAs(date2);
-
- assertThat(date1).isEqualToIgnoringSeconds(date3);
-
- assertThat(date1).isInSameMinuteAs(date3);
-
- assertThat(date1).isEqualToIgnoringMinutes(date4);
-
- assertThat(date1).isInSameHourAs(date4);
-
- assertThat(date1).isEqualToIgnoringHours(date5);
-
- assertThat(date1).isInSameDayAs(date5);
- }
对List断言
- @Test
- public void testList() {
-
- assertThat(newArrayList()).isEmpty();
-
- assertThat(newArrayList(1, 2, 3)).startsWith(1).endsWith(3);
-
- assertThat(newArrayList(1, 2, 3)).contains(1, atIndex(0)).contains(2, atIndex(1)).contains(3)
- .isSorted();
-
- assertThat(newArrayList(3, 1, 2)).isSubsetOf(newArrayList(1, 2, 3, 4));
-
- assertThat(Lists.newArrayList("a", "b", "c")).containsOnlyOnce("a");
- }
对Map断言
- @Test
- public void testMap() {
- Map<String, Object> foo = Maps.newHashMap();
- foo.put("A", 1);
- foo.put("B", 2);
- foo.put("C", 3);
-
-
- assertThat(foo).isNotEmpty().hasSize(3);
-
- assertThat(foo).contains(entry("A", 1), entry("B", 2));
-
- assertThat(foo).containsKeys("A", "B", "C");
-
- assertThat(foo).containsValue(3);
- }
对Class断言
- @Test
- public void testClass() {
-
- assertThat(Magical.class).isAnnotation();
-
- assertThat(Ring.class).isNotAnnotation();
-
- assertThat(Ring.class).hasAnnotation(Magical.class);
-
- assertThat(Ring.class).isNotInterface();
-
- assertThat("string").isInstanceOf(String.class);
-
- assertThat(Person.class).isAssignableFrom(Employee.class);
- }
-
- @Magical
- public enum Ring {
- oneRing, vilya, nenya, narya, dwarfRing, manRing;
- }
- @Target(ElementType.TYPE)
- @Retention(RetentionPolicy.RUNTIME)
- public @interface Magical {}
- public class Person {}
- public class Employee extends Person {}
使用fail方法
- @Test
- public void testFail() {
- try {
- fail("在不检查任何条件的情况下使断言失败。显示一则消息");
- } catch (AssertionError ae) {
- logger.info("可以通过catch捕获该Error");
- }
- try {
- failBecauseExceptionWasNotThrown(ServiceException.class);
- } catch (AssertionError ae) {
- logger.info("可以通过catch捕获该Error");
- }
- }
除此之外,还提供包括Exception、Iterable、JodaTime、Guava等等很多的断言支持。