public class

JUnitMatchers

extends Object
java.lang.Object
   ↳ org.junit.matchers.JUnitMatchers

Class Overview

Convenience import class: these are useful matchers for use with the assertThat method, but they are not currently included in the basic CoreMatchers class from hamcrest.

Summary

Public Constructors
JUnitMatchers()
Public Methods
static <T> CombinableMatcher<T> both(Matcher<T> matcher)
This is useful for fluently combining matchers that must both pass.
static Matcher<String> containsString(String substring)
static <T> CombinableMatcher<T> either(Matcher<T> matcher)
This is useful for fluently combining matchers where either may pass, for example:
   assertThat(string, both(containsString("a")).and(containsString("b")));
 
static <T> Matcher<Iterable<T>> everyItem(Matcher<T> elementMatcher)
static <T> Matcher<Iterable<T>> hasItem(T element)
static <T> Matcher<Iterable<T>> hasItem(Matcher<? extends T> elementMatcher)
static <T> Matcher<Iterable<T>> hasItems(T... elements)
static <T> Matcher<Iterable<T>> hasItems(Matcher...<? extends T> elementMatchers)
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public JUnitMatchers ()

Public Methods

public static CombinableMatcher<T> both (Matcher<T> matcher)

This is useful for fluently combining matchers that must both pass. For example:

   assertThat(string, both(containsString("a")).and(containsString("b")));
 

public static Matcher<String> containsString (String substring)

Returns
  • a matcher matching any string that contains substring

public static CombinableMatcher<T> either (Matcher<T> matcher)

This is useful for fluently combining matchers where either may pass, for example:

   assertThat(string, both(containsString("a")).and(containsString("b")));
 

public static Matcher<Iterable<T>> everyItem (Matcher<T> elementMatcher)

Returns
  • A matcher matching any collection in which every element matches elementMatcher

public static Matcher<Iterable<T>> hasItem (T element)

Returns
  • A matcher matching any collection containing element

public static Matcher<Iterable<T>> hasItem (Matcher<? extends T> elementMatcher)

Returns
  • A matcher matching any collection containing an element matching elementMatcher

public static Matcher<Iterable<T>> hasItems (T... elements)

Returns
  • A matcher matching any collection containing every element in elements

public static Matcher<Iterable<T>> hasItems (Matcher...<? extends T> elementMatchers)

Returns
  • A matcher matching any collection containing at least one element that matches each matcher in elementMatcher (this may be one element matching all matchers, or different elements matching each matcher)