public class TestUtils extends Object
| Modifier and Type | Method and Description |
|---|---|
static void |
assertException(Class<? extends Throwable> expectedThrowable,
Runnable runnable)
A utility method to assert the expected exception thrown by a given procedure, that is,
a function that accepts no arguments and returns void (e.g., a Runnable's
run()
method). |
static void |
assertException(Class<? extends Throwable> expectedThrowable,
String expectedMessage,
Class<? extends Throwable> expectedCause,
Runnable runnable)
A utility method to assert the expected exception, message and cause thrown by a given
procedure, that is, a function that accepts no arguments and returns void (e.g., a
Runnable's
run() method). |
static void |
assertException(Class<? extends Throwable> expectedThrowable,
String expectedMessage,
Class<? extends Throwable> expectedCause,
Supplier<?> supplier)
A utility method to assert the expected exception, message and cause thrown by a
supplying function.
|
static void |
assertException(Class<? extends Throwable> expectedThrowable,
String expectedMessage,
Class<? extends Throwable> expectedCause,
Throwable throwable)
A utility method to assert that a given throwable matches the expected class, cause and
message.
|
static void |
assertException(Class<? extends Throwable> expectedThrowable,
String expectedMessage,
Runnable runnable)
A utility method to assert the expected exception and message thrown by a given
procedure, that is, a function that accepts no arguments and returns void (e.g., a
Runnable's
run() method). |
static void |
assertException(Class<? extends Throwable> expectedThrowable,
String expectedMessage,
Supplier<?> supplier)
A utility method to assert the expected exception and message thrown by a supplying
function.
|
static void |
assertException(Class<? extends Throwable> expectedThrowable,
String expectedMessage,
Throwable throwable)
A utility method to assert that a given throwable matches the expected class and
message.
|
static void |
assertException(Class<? extends Throwable> expectedThrowable,
Supplier<?> supplier)
A utility method to assert the expected exception thrown by a supplying function.
|
static void |
assertException(Class<? extends Throwable> expectedThrowable,
Throwable throwable)
A utility method to assert that a given throwable matches the expected class.
|
static void |
assertInstantiationNotAllowed(Class<?> targetClass)
Tests that no instances of an utility class are created.
|
static void |
assertInstantiationNotAllowed(Class<?> targetClass,
Class<? extends Throwable> expectedThrowableClass)
Tests that no instances of an utility class are created.
|
static void |
assertInstantiationNotAllowed(Class<?> targetClass,
Class<? extends Throwable> expectedThrowableClass,
String expectedErrorMessage)
Tests that no instances of an utility class are created.
|
static void |
assertStringContains(String testString,
String... expectedStrings)
Tests that the given test string contains all of the expected strings.
|
static void |
assertStringDoesNotContain(String testString,
String... expectedStrings)
Tests that the given test string does not contain any of the expected strings.
|
public static void assertInstantiationNotAllowed(Class<?> targetClass) throws ReflectiveOperationException
targetClass - the class to be testedAssertionError - if the target class is instantiatedReflectiveOperationException - in case of errors getting constructor metadata or
instantiating the private constructorpublic static void assertInstantiationNotAllowed(Class<?> targetClass, Class<? extends Throwable> expectedThrowableClass) throws ReflectiveOperationException
targetClass - the class to be testedexpectedThrowableClass - the expected throwable to be checked in case the
constructor is calledAssertionError - if the target class is instantiated or a different
throwable class is receivedReflectiveOperationException - in case of errors getting constructor metadata or
instantiating the private constructorpublic static void assertInstantiationNotAllowed(Class<?> targetClass, Class<? extends Throwable> expectedThrowableClass, String expectedErrorMessage) throws ReflectiveOperationException
targetClass - the class to be testedexpectedThrowableClass - the expected throwable to be checked in case the
constructor is calledexpectedErrorMessage - the expected error message to be checked in case the
constructor is calledAssertionError - if the target class is instantiated or a different
throwable class is receivedReflectiveOperationException - in case of errors getting constructor metadata or
instantiating the private constructorpublic static void assertException(Class<? extends Throwable> expectedThrowable, Throwable throwable)
expectedThrowable - the expected throwable classthrowable - the throwable to be validatedpublic static void assertException(Class<? extends Throwable> expectedThrowable, String expectedMessage, Throwable throwable)
expectedThrowable - the expected throwable classexpectedMessage - the expected message (if applicable)throwable - the throwable to be validatedpublic static void assertException(Class<? extends Throwable> expectedThrowable, String expectedMessage, Class<? extends Throwable> expectedCause, Throwable throwable)
expectedThrowable - the expected throwable classexpectedMessage - the expected message (if applicable)expectedCause - the expected throwable cause class (if applicable)throwable - the throwable to be validatedpublic static void assertException(Class<? extends Throwable> expectedThrowable, Supplier<?> supplier)
Example of usage:
assertException(IllegalArgumentException.class,
() -> TestSubject.testMethod("param1"))
expectedThrowable - the expected throwable classsupplier - the supplying function that throws an exception to be
validatedpublic static void assertException(Class<? extends Throwable> expectedThrowable, String expectedMessage, Supplier<?> supplier)
Example of usage:
assertException(IllegalArgumentException.class, "Invalid argument: param1",
() -> TestSubject.testMethod("param1"))
expectedThrowable - the expected throwable classexpectedMessage - the expected message (if applicable)supplier - the supplying function that throws an exception to be
validatedpublic static void assertException(Class<? extends Throwable> expectedThrowable, String expectedMessage, Class<? extends Throwable> expectedCause, Supplier<?> supplier)
Example of usage:
assertException(MyException.class, "Unable to process the request",
IllegalStateException.class() -> TestSubject.process("request1"))
expectedThrowable - the expected throwable classexpectedMessage - the expected message (if applicable)expectedCause - the expected throwable cause class (if applicable)supplier - the supplying function that throws an exception to be
validatedpublic static void assertException(Class<? extends Throwable> expectedThrowable, Runnable runnable)
run()
method).
Example of usage:
assertException(IllegalStateException.class, () -> thread.start())
expectedThrowable - the expected throwable classrunnable - the runnable that produces an exception to be validatedpublic static void assertException(Class<? extends Throwable> expectedThrowable, String expectedMessage, Runnable runnable)
run() method).
Example of usage:
assertException(IllegalStateException.class, "Already started",
() -> thread.start())
expectedThrowable - the expected throwable classexpectedMessage - the expected message (if applicable)runnable - the runnable that produces an exception to be validatedpublic static void assertException(Class<? extends Throwable> expectedThrowable, String expectedMessage, Class<? extends Throwable> expectedCause, Runnable runnable)
run() method).
Example of usage:
assertException(MyException.class, "Already started",
IllegalStateException.class() -> thread.start())
expectedThrowable - the expected throwable classexpectedMessage - the expected message (if applicable)expectedCause - the expected throwable cause class (if applicable)runnable - the runnable that produces an exception to be validatedpublic static void assertStringContains(String testString, String... expectedStrings)
testString - the string to be testedexpectedStrings - the strings to be expected inside the testStringpublic static void assertStringDoesNotContain(String testString, String... expectedStrings)
testString - the string to be testedexpectedStrings - the strings not to be expected inside the testStringCopyright © 2021. All rights reserved.