jck28-lucio-junit5结合高级断言-hamcrest

pom文件导入:

<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest</artifactId>
    <version>2.2</version>
    <scope>test</scope>
</dependency>

Hamcrest

  • Common Core Matchers
    • 常用断言
    • is
    • equalTo
    • not
    • hasItem
    • allOf
    • anyOf
    • both
    • either

Common Core Matchers

is

  • is(T)
    • 将一个对象作为参数来检查相等性
  • is(Matcher<T>)
    • 使用另一个匹配器,使相等性语句更具表现力
@Test
void UsingIsForMatch(){
    String testString = "hamcrest core Is match";

    assertThat(testString, is("hamcrest core Is match"));
    assertThat(testString, is(equalTo("hamcrest core Is match")));
}

equalTo(T)

  • 将一个对象作为参数并检查其与另一个对象的相等性{style=width:500px}
  • 经常与 is(Matcher<T>) 一起使用{style=width:500px}
@Test
void UsingEqualToForMatch(){
    String actualString = "equalTo match";
    List<String> actualList = Arrays.asList("equalTo", "match");
    Object original = 100;

    assertThat(actualString, is(equalTo("equalTo match")));
    assertThat(actualList, is(equalTo(Arrays.asList("equalTo", "match"))));
    assertThat(original, equalToObject(100));
}

not

  • 检查给定对象的不相等性
  • not(T)
    • 接受一个对象作为参数
  • not(Matcher<T>)
    • 接受另一个匹配器
@Test
void UsingNotForMatch(){
    String testString = "hamcrest not match";

    assertThat(testString, not("hamcrest other match"));
    assertThat(testString, is(not(equalTo("hamcrest other match"))));
    assertThat(testString, is(not(instanceOf(Integer.class))));

}

hasItem

  • hasItem(T)
  • hasItem(Matcher<? extends T>)
  • 检查的 Iterable 集合是否与给定对象或匹配器匹配{style=width:500px}
  • 也可以对多个项目进行断言{style=width:500px}
@Test
void UsingHasItemForMatch(){
    List<String> list = Arrays.asList("java", "hamcrest", "JUnit5");

    assertThat(list, hasItem("java"));
    assertThat(list, hasItem(isA(String.class)));

    assertThat(list, hasItems("java", "JUnit5"));
    assertThat(list, hasItems(isA(String.class), endsWith("est")));
}

allOf

  • allOf(Matcher<? extends T>…)
  • 断言实际对象是否与所有指定条件匹配{style=width:500px}
@Test
void UsingAllOfForMatch(){
    String testString = "Achilles is powerful";
    assertThat(testString, allOf(
      startsWith("Achi"), endsWith("ul"), containsString("Achilles")));

}

anyOf

  • anyOf(Matcher<? extends T>…)
  • 检查的对象匹配任何指定的条件,则匹配{style=width:500px}
@Test
void UsingAllOfForMatch(){
    String testString2 = "Hector killed Achilles";
    assertThat(testString2, anyOf(startsWith("Hec"), containsString("baeldung")));
}

both

  • both(Matcher<? extends T>)
    • and 配合使用{style=width:500px}
  • 两个指定条件都匹配检查对象时匹配{style=width:500px}
@Test
void UsingBothForMatch(){
    String testString = "daenerys targaryen";
    assertThat(testString, both(startsWith("daene")).and(containsString("yen")));

}

either

  • either(Matcher<? extends T>)
    • or 配合使用{style=width:500px}
  • 任一指定条件与检查对象匹配时匹配{style=width:500px}
@Test
void UsingBothForMatch(){
    String testString = "daenerys targaryen";
    assertThat(testString, both(startsWith("daene")).and(containsString("yen")));
    assertThat(testString, either(startsWith("tar")).or(containsString("targaryen")));

}