Class ExtraMatchers


  • public class ExtraMatchers
    extends java.lang.Object
    Extra hamcrest matchers.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> org.hamcrest.Matcher<java.util.Collection<T>> allItemsMatch​(org.hamcrest.Matcher<T> itemMatcher)
      Matcher that checks all items in a collection to match the item-matcher.
      static <T> org.hamcrest.Matcher<java.util.Set<T>> distinctFrom​(java.util.Set<T> from)
      Matcher to check that two sets does not have any common elements.
      static org.hamcrest.Matcher<java.lang.String> equalIgnoreIndent​(java.lang.String expected)
      Equivalent to 'equalToLines' but also ignores any indentation each line has.
      static org.hamcrest.Matcher<java.lang.String> equalToLines​(java.lang.String expected)
      Equivalent to 'equalTo' for strings that normalize the new-line to '\n', this can make testing that should match output on various platforms easier to wrote.
      static <T extends java.lang.Number>
      org.hamcrest.Matcher<T>
      inRange​(T lowerInclusive, T upperExclusive)
      Checks that the value is in a given numeric value range.
      static org.hamcrest.Matcher<java.lang.String> matchesRegex​(java.lang.String pattern)
      Matcher that uses a pattern to match against a string.
      static org.hamcrest.Matcher<java.lang.String> matchesRegex​(java.util.regex.Pattern pattern)
      Matcher that uses a pattern to match against a string.
      static <T> org.hamcrest.Matcher<T> oneOf​(T... alternatives)
      Alternative to hamcrest 'oneOf' that simplifies to match any one of the values given.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • inRange

        public static <T extends java.lang.Number> org.hamcrest.Matcher<T> inRange​(@Nonnull
                                                                                   T lowerInclusive,
                                                                                   @Nonnull
                                                                                   T upperExclusive)
        Checks that the value is in a given numeric value range.
        Type Parameters:
        T - The number type.
        Parameters:
        lowerInclusive - The lower inclusive accepted value.
        upperExclusive - The upper exclusive accepted value (the exact value not accepted).
        Returns:
        The matcher.
      • equalToLines

        public static org.hamcrest.Matcher<java.lang.String> equalToLines​(@Nonnull
                                                                          java.lang.String expected)
        Equivalent to 'equalTo' for strings that normalize the new-line to '\n', this can make testing that should match output on various platforms easier to wrote.
        Parameters:
        expected - The expected line content.
        Returns:
        The matcher.
      • equalIgnoreIndent

        public static org.hamcrest.Matcher<java.lang.String> equalIgnoreIndent​(@Nonnull
                                                                               java.lang.String expected)
        Equivalent to 'equalToLines' but also ignores any indentation each line has.
        Parameters:
        expected - The expected line content.
        Returns:
        The matcher.
      • oneOf

        @SafeVarargs
        public static <T> org.hamcrest.Matcher<T> oneOf​(T... alternatives)
        Alternative to hamcrest 'oneOf' that simplifies to match any one of the values given. And uses a vararg interface
        Type Parameters:
        T - The value type.
        Parameters:
        alternatives - The value alternatives.
        Returns:
        The matcher.
      • distinctFrom

        public static <T> org.hamcrest.Matcher<java.util.Set<T>> distinctFrom​(java.util.Set<T> from)
        Matcher to check that two sets does not have any common elements. The elements must have proper equals method implementations for this matcher to work.
        Type Parameters:
        T - The element type.
        Parameters:
        from - The set it should not match.
        Returns:
        The distinct-from matcher.
      • allItemsMatch

        public static <T> org.hamcrest.Matcher<java.util.Collection<T>> allItemsMatch​(org.hamcrest.Matcher<T> itemMatcher)
        Matcher that checks all items in a collection to match the item-matcher.
        Type Parameters:
        T - The item type.
        Parameters:
        itemMatcher - The matcher for each individual item.
        Returns:
        The collection matcher.
      • matchesRegex

        public static org.hamcrest.Matcher<java.lang.String> matchesRegex​(java.lang.String pattern)
        Matcher that uses a pattern to match against a string.
        Parameters:
        pattern - The pattern to use.
        Returns:
        The matcher.
      • matchesRegex

        public static org.hamcrest.Matcher<java.lang.String> matchesRegex​(java.util.regex.Pattern pattern)
        Matcher that uses a pattern to match against a string.
        Parameters:
        pattern - The pattern to use.
        Returns:
        The matcher.