public class InstantiationNotAllowedMatcher extends org.hamcrest.TypeSafeDiagnosingMatcher<Class<?>>
First, the matcher verifies that all declared constructors are private, then it tries to produce a new instance using the default constructor, via Reflection.
A matching class should have all constructors declared as private and
throw an exception, so the class will never be instantiated.
For example:
private MyClass()
{
throw new UnsupportedOperationException("Instantiation not allowed");
}
| Constructor and Description |
|---|
InstantiationNotAllowedMatcher() |
| Modifier and Type | Method and Description |
|---|---|
void |
describeTo(org.hamcrest.Description description)
Describes the "expected" pat of the test description.
|
static InstantiationNotAllowedMatcher |
instantiationNotAllowed()
Creates a matcher that matches if the examined class cannot be instantiated, which is
particularly useful for utility classes.
|
InstantiationNotAllowedMatcher |
throwing(Class<? extends Exception> exception)
Assigns an expected Exception (optional step).
|
InstantiationNotAllowedMatcher |
withMessage(org.hamcrest.Matcher<String> matcher)
Assigns an external Matcher to be used in combination for the exception message
validation (optional).
|
InstantiationNotAllowedMatcher |
withMessage(String message)
Assigns an expected message for Exception validation (optional).
|
public static InstantiationNotAllowedMatcher instantiationNotAllowed()
For example:
assertThat(TestUtils.class, instantiationNotAllowed());
public InstantiationNotAllowedMatcher throwing(Class<? extends Exception> exception)
For example:
assertThat(TestUtils.class, instantiationNotAllowed()
.throwing(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 constructor throws a NullPointerException, all of the
following assertions are valid:
throwing(IllegalStateException.class); throwing(RuntimeException.class); throwing(Exception.class);
In other words, the matcher tests whether the actual exception can be converted to the specified class.
exception - the expected exception class; must not be nullNullPointerException - if the specified class is nullpublic InstantiationNotAllowedMatcher withMessage(String message)
For example:
assertThat(TestUtils.class, instantiationNotAllowed()
.throwing(IllegalStateException.class)
.withMessage("instantiation not allowed"));
The following example is also valid for a test where any Exception is
acceptable, provided that the message matches:
assertThat(TestUtils.class, instantiationNotAllowed()
.withMessage("instantiation not allowed"));
message - the message for exception validationpublic InstantiationNotAllowedMatcher withMessage(org.hamcrest.Matcher<String> matcher)
For example:
assertThat(TestUtils.class, instantiationNotAllowed()
.throwing(IllegalStateException.class)
.withMessage(containsString("not allowed")));
The following example is also valid for a test where any Exception is
acceptable, provided that the message matches:
assertThat(TestUtils.class, instantiationNotAllowed()
.withMessage(endsWith("not allowed")));
matcher - the matcher to be used in combination for exception message validationpublic void describeTo(org.hamcrest.Description description)
description - the Description to be appended toCopyright © 2024. All rights reserved.