public class AdvancedMatchers extends Object
For example:
import static net.obvj.junit.utils.matchers.AdvancedMatchers.*;
...
assertThat(() -> obj.doStuff(),
throwsException(IllegalStateException.class)
.withMessage(containsAll("invalid state").ignoreCase()));
| Modifier and Type | Method and Description |
|---|---|
static StringMatcher |
containsAll(String... substrings)
Creates a matcher that matches if the examined string contains all of the
specified substrings (regardless of the order they appear in the string).
|
static StringMatcher |
containsAny(String... substrings)
Creates a matcher that matches if the examined string contains any of the
specified substrings.
|
static StringMatcher |
containsNone(String... substrings)
Creates a matcher that matches if the examined string contains none of the
specified substrings.
|
static InstantiationNotAllowedMatcher |
instantiationNotAllowed()
Creates a matcher that matches if the examined class cannot be instantiated, which is
particularly useful for utility classes.
|
static ExceptionMatcher |
throwsException(Class<? extends Exception> exception)
Creates a matcher that matches if the examined procedure throws a given exception.
|
public static ExceptionMatcher throwsException(Class<? extends Exception> exception)
For example:
assertThat(() -> obj.doStuff("p1"),
throwsException(IllegalStateException.class));
The matcher matches if the actual exception class is either the same as, or is a child
of, the Exception represented by the specified parameter.
For example, if the examined code throws a NullPointerException, all of the
following assertions are valid:
assertThat( ... throwsException(NullPointerException.class));
assertThat( ... throwsException(RuntimeException.class));
assertThat( ... throwsException(Exception.class));
In other words, the matcher tests whether the actual exception can be converted to the
specified class.
If applicable, the matcher can also be incremented to validate the exception message and cause.
For example:
assertThat(() -> obj.doStuff(""),
throwsException(IllegalArgumentException.class)
.withMessage("The argument must not be empty"));
exception - the expected exception class. A null value is allowed, and means that
no exception is expectedpublic static InstantiationNotAllowedMatcher instantiationNotAllowed()
For example:
assertThat(TestUtils.class, instantiationNotAllowed());
First, the matcher verifies all declared constructors, and then it tries to create a new instance using the default constructor.
A matching class shall have all constructors declared as private and throw an
exception inside the default constructor, so it can never be instantiated.
For example:
private MyClass()
{
throw new IllegalStateException("Instantiation not allowed");
}
If applicable, the matcher can also be incremented to validate the exception thrown by
the constructor:
assertThat(TestUtils.class, instantiationNotAllowed()
.throwing(IllegalStateException.class)
.withMessage("Instantiation not allowed"));
public static StringMatcher containsAll(String... substrings)
For example:
assertThat("the quick brown fox", containsAll("fox", "the"))
By default, the matcher is case-sensitive, but this behavior can be modified
with an additional method call.
For example:
assertThat("the quick brown fox", containsAll("FOX", "The").ignoreCase())
substrings - the substrings to be testedpublic static StringMatcher containsAny(String... substrings)
For example:
assertThat("the quick brown fox", containsAny("fox", "dragon"))
By default, the matcher is case-sensitive, but this behavior can be modified
with an additional method call.
For example:
assertThat("the quick brown fox", containsAny("FOX", "dragon").ignoreCase())
substrings - the substrings to be testedpublic static StringMatcher containsNone(String... substrings)
For example:
assertThat("the quick brown fox", containsNone("cat", "mouse"))
substrings - the substrings to be testedCopyright © 2021. All rights reserved.