Hamcrest Text Matchers

Hamcrest Text Matchers

1. Vue d'ensemble

Dans ce didacticiel, nous allons explorer les correspondeurs de texte Hamcrest.

Nous avons déjà discuté des Matchers Hamcrest en général danstesting with Hamcrest, dans ce tutoriel, nous nous concentrerons uniquement sur les MatchersText.

2. Configuration Maven

Tout d'abord, nous devons ajouter la dépendance suivante à nospom.xml:


    org.hamcrest
    java-hamcrest
    2.0.0.0
    test

La dernière version dejava-hamcrest peut être téléchargée depuis Maven Central.

Maintenant, nous allons plonger directement dans les correspondeurs de texte Hamcrest.

3. Correspondants d'égalité de texte

Nous pouvons, bien sûr, vérifier si deux chaînes sont égales en utilisant le matcher standardisEqual().

De plus, nous avons deux matchers spécifiques aux typesString:equalToIgnoringCase() etequalToIgnoringWhiteSpace().

Vérifions si deuxStrings sont égaux - en ignorant la casse:

@Test
public void whenTwoStringsAreEqual_thenCorrect() {
    String first = "hello";
    String second = "Hello";

    assertThat(first, equalToIgnoringCase(second));
}

Nous pouvons également vérifier si deuxStrings sont égaux - en ignorant les espaces de début et de fin:

@Test
public void whenTwoStringsAreEqualWithWhiteSpace_thenCorrect() {
    String first = "hello";
    String second = "   Hello   ";

    assertThat(first, equalToIgnoringWhiteSpace(second));
}

4. Correspondants de texte vides

Nous pouvons vérifier si unString est vide, ce qui signifie qu'il ne contient que des espaces, en utilisant les matchersblankString() etblankOrNullString():

@Test
public void whenStringIsBlank_thenCorrect() {
    String first = "  ";
    String second = null;

    assertThat(first, blankString());
    assertThat(first, blankOrNullString());
    assertThat(second, blankOrNullString());
}

Par contre, si nous voulons vérifier si unString est vide, nous pouvons utiliser les matchersemptyString():

@Test
public void whenStringIsEmpty_thenCorrect() {
    String first = "";
    String second = null;

    assertThat(first, emptyString());
    assertThat(first, emptyOrNullString());
    assertThat(second, emptyOrNullString());
}

5. Correspondants de motifs

Nous pouvons également vérifier si un texte donné correspond à une expression régulière en utilisant la fonctionmatchesPattern():

@Test
public void whenStringMatchPattern_thenCorrect() {
    String first = "hello";

    assertThat(first, matchesPattern("[a-z]+"));
}

6. Matchers de sous-chaîne

Nous pouvons déterminer si un texte contient un autre sous-texte en utilisant la fonctioncontainsString() oucontainsStringIgnoringCase():

@Test
public void whenVerifyStringContains_thenCorrect() {
    String first = "hello";

    assertThat(first, containsString("lo"));
    assertThat(first, containsStringIgnoringCase("EL"));
}

Si nous nous attendons à ce que les sous-chaînes soient dans un ordre spécifique, nous pouvons appeler le matcherstringContainsInOrder():

@Test
public void whenVerifyStringContainsInOrder_thenCorrect() {
    String first = "hello";

    assertThat(first, stringContainsInOrder("e","l","o"));
}

Voyons ensuite comment vérifier qu'unString commence par unString donné:

@Test
public void whenVerifyStringStartsWith_thenCorrect() {
    String first = "hello";

    assertThat(first, startsWith("he"));
    assertThat(first, startsWithIgnoringCase("HEL"));
}

Et enfin, nous pouvons vérifier si unString se termine par unString spécifié:

@Test
public void whenVerifyStringEndsWith_thenCorrect() {
    String first = "hello";

    assertThat(first, endsWith("lo"));
    assertThat(first, endsWithIgnoringCase("LO"));
}

7. Conclusion

Dans ce rapide didacticiel, nous avons exploré Hamcrest Text Matchers.

Comme toujours, le code source complet des exemples peut être trouvéover on GitHub.