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 IsNegativeMatcher |
isNegative()
A matcher that matches if a
Number is negative. |
static IsPositiveMatcher |
isPositive()
A matcher that matches if a
Number is positive. |
static ExceptionMatcher |
throwsException()
Creates a matcher that matches if the examined procedure throws any exception.
|
static ExceptionMatcher |
throwsException(Class<? extends Exception> exception)
Creates a matcher that matches if the examined procedure throws a given exception.
|
static org.hamcrest.Matcher<Procedure> |
throwsNoException()
Creates a matcher that matches if the examined procedure throws no exception.
|
public static ExceptionMatcher throwsException()
For example:
Note: This has the same effect as calling:assertThat(() -> obj.doStuff("p1"), throwsException());
assertThat(() -> obj.doStuff("p1"),throwsException(Exception.class));
public static ExceptionMatcher throwsException(Class<? extends Exception> exception)
For example:
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.assertThat(() -> obj.doStuff("p1"),throwsException(IllegalStateException.class));
For example, if the examined code throws a NullPointerException, all of the
following assertions are valid:
In other words, the matcher tests whether the actual exception can be converted to the specified class.assertThat( ... throwsException(NullPointerException.class));assertThat( ... throwsException(RuntimeException.class));assertThat( ... throwsException(Exception.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 org.hamcrest.Matcher<Procedure> throwsNoException()
For example:
Note: This has the same effect as calling:assertThat(() -> obj.doStuff("p1"), throwsNoException());
assertThat(() -> obj.doStuff("p1"), throwsException(null));
public 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 testedpublic static IsPositiveMatcher isPositive()
Number is positive.
This is particularly useful in situations where the exact value of an operation is unpredictable.
For example:
assertThat(stopwatch.elapsedTime(TimeUnit.MILLISECONDS), isPositive());
public static IsNegativeMatcher isNegative()
Number is negative.
This is particularly useful in situations where the exact value of an operation is unpredictable.
For example:
assertThat(duration.compareTo(otherDuration), isNegative());
Copyright © 2023. All rights reserved.