zoukankan      html  css  js  c++  java
  • 断言AssertJ使用方式

     1 // entry point for all assertThat methods and utility methods (e.g. entry)
     2 import static org.assertj.core.api.Assertions.*;
     3 
     4 // basic assertions
     5 assertThat(frodo.getName()).isEqualTo("Frodo");
     6 assertThat(frodo).isNotEqualTo(sauron);
     7 
     8 // chaining string specific assertions
     9 assertThat(frodo.getName()).startsWith("Fro")
    10                            .endsWith("do")
    11                            .isEqualToIgnoringCase("frodo");
    12 
    13 // collection specific assertions (there are plenty more)
    14 // in the examples below fellowshipOfTheRing is a List<TolkienCharacter>
    15 assertThat(fellowshipOfTheRing).hasSize(9)
    16                                .contains(frodo, sam)
    17                                .doesNotContain(sauron);
    18 
    19 // as() is used to describe the test and will be shown before the error message
    20 assertThat(frodo.getAge()).as("check %s's age", frodo.getName()).isEqualTo(33);
    21 
    22 // Java 8 exception assertion, standard style ...
    23 assertThatThrownBy(() -> { throw new Exception("boom!"); }).hasMessage("boom!");
    24 // ... or BDD style
    25 Throwable thrown = catchThrowable(() -> { throw new Exception("boom!"); });
    26 assertThat(thrown).hasMessageContaining("boom");
    27 
    28 // using the 'extracting' feature to check fellowshipOfTheRing character's names (Java 7)
    29 assertThat(fellowshipOfTheRing).extracting("name")
    30                                .contains("Boromir", "Gandalf", "Frodo", "Legolas")
    31 // same thing using a Java 8 method reference
    32 assertThat(fellowshipOfTheRing).extracting(TolkienCharacter::getName)
    33                                .doesNotContain("Sauron", "Elrond");
    34 
    35 // extracting multiple values at once grouped in tuples (Java 7)
    36 assertThat(fellowshipOfTheRing).extracting("name", "age", "race.name")
    37                                .contains(tuple("Boromir", 37, "Man"),
    38                                          tuple("Sam", 38, "Hobbit"),
    39                                          tuple("Legolas", 1000, "Elf"));
    40 
    41 // filtering a collection before asserting in Java 7 ... 
    42 assertThat(fellowshipOfTheRing).filteredOn("race", HOBBIT)
    43                                .containsOnly(sam, frodo, pippin, merry);
    44 // ... or in Java 8
    45 assertThat(fellowshipOfTheRing).filteredOn(character -> character.getName().contains("o"))
    46                                .containsOnly(aragorn, frodo, legolas, boromir);
    47 
    48 // combining filtering and extraction (yes we can)
    49 assertThat(fellowshipOfTheRing).filteredOn(character -> character.getName().contains("o"))
    50                                .containsOnly(aragorn, frodo, legolas, boromir)
    51                                .extracting(character -> character.getRace().getName())
    52                                .contains("Hobbit", "Elf", "Man");

    文档地址:http://joel-costigliola.github.io/assertj/index.html

  • 相关阅读:
    Java Lambda 表达式 对 Map 对象排序
    比较两个list对象是否相同
    ubuntu redis 自启动配置文件(关机有密码)
    spring中订阅redis键值过期消息通知
    网站架构之性能优化(转)
    Json转Java Bean
    spring mvc 4 校验
    java @ResponseBody返回值中去掉NULL字段
    合并两个java bean对象非空属性(泛型)
    spring mvc 删除返回字符串中值为null的字段
  • 原文地址:https://www.cnblogs.com/HCT118/p/7865424.html
Copyright © 2011-2022 走看看