Package net.obvj.junit.utils
Class TestUtils
java.lang.Object
net.obvj.junit.utils.TestUtils
public class TestUtils
extends java.lang.Object
Common utilities for working with unit tests.
- Since:
- 2.0
- Author:
- oswaldo.bapvic.jr
-
Method Summary
Modifier and Type Method Description static voidassertException(java.lang.Class<? extends java.lang.Throwable> expectedThrowable, java.lang.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'srun()method).static voidassertException(java.lang.Class<? extends java.lang.Throwable> expectedThrowable, java.lang.String expectedMessage, java.lang.Class<? extends java.lang.Throwable> expectedCause, java.lang.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'srun()method).static voidassertException(java.lang.Class<? extends java.lang.Throwable> expectedThrowable, java.lang.String expectedMessage, java.lang.Class<? extends java.lang.Throwable> expectedCause, java.lang.Throwable throwable)A utility method to assert that a given throwable matches the expected class, cause and message.static voidassertException(java.lang.Class<? extends java.lang.Throwable> expectedThrowable, java.lang.String expectedMessage, java.lang.Class<? extends java.lang.Throwable> expectedCause, java.util.function.Supplier<?> supplier)A utility method to assert the expected exception, message and cause thrown by a supplying function.static voidassertException(java.lang.Class<? extends java.lang.Throwable> expectedThrowable, java.lang.String expectedMessage, java.lang.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'srun()method).static voidassertException(java.lang.Class<? extends java.lang.Throwable> expectedThrowable, java.lang.String expectedMessage, java.lang.Throwable throwable)A utility method to assert that a given throwable matches the expected class and message.static voidassertException(java.lang.Class<? extends java.lang.Throwable> expectedThrowable, java.lang.String expectedMessage, java.util.function.Supplier<?> supplier)A utility method to assert the expected exception and message thrown by a supplying function.static voidassertException(java.lang.Class<? extends java.lang.Throwable> expectedThrowable, java.lang.Throwable throwable)A utility method to assert that a given throwable matches the expected class.static voidassertException(java.lang.Class<? extends java.lang.Throwable> expectedThrowable, java.util.function.Supplier<?> supplier)A utility method to assert the expected exception thrown by a supplying function.static voidassertNoInstancesAllowed(java.lang.Class<?> targetClass)Tests that no instances of an utility class are created.static voidassertNoInstancesAllowed(java.lang.Class<?> targetClass, java.lang.Class<? extends java.lang.Throwable> expectedThrowableClass)Tests that no instances of an utility class are created.static voidassertNoInstancesAllowed(java.lang.Class<?> targetClass, java.lang.Class<? extends java.lang.Throwable> expectedThrowableClass, java.lang.String expectedErrorMessage)Tests that no instances of an utility class are created.static voidassertStringContains(java.lang.String testString, java.lang.String... expectedStrings)Tests that the given test string contains all of the expected strings.static voidassertStringDoesNotContain(java.lang.String testString, java.lang.String... expectedStrings)Tests that the given test string does not contain any of the expected strings.
-
Method Details
-
assertNoInstancesAllowed
public static void assertNoInstancesAllowed(java.lang.Class<?> targetClass) throws java.lang.ReflectiveOperationExceptionTests that no instances of an utility class are created.- Parameters:
targetClass- the class to be tested- Throws:
java.lang.AssertionError- if the target class is instantiatedjava.lang.ReflectiveOperationException- in case of errors getting constructor metadata or instantiating the private constructor
-
assertNoInstancesAllowed
public static void assertNoInstancesAllowed(java.lang.Class<?> targetClass, java.lang.Class<? extends java.lang.Throwable> expectedThrowableClass) throws java.lang.ReflectiveOperationExceptionTests that no instances of an utility class are created.- Parameters:
targetClass- the class to be testedexpectedThrowableClass- the expected throwable to be checked in case the constructor is called- Throws:
java.lang.AssertionError- if the target class is instantiated or a different throwable class is receivedjava.lang.ReflectiveOperationException- in case of errors getting constructor metadata or instantiating the private constructor
-
assertNoInstancesAllowed
public static void assertNoInstancesAllowed(java.lang.Class<?> targetClass, java.lang.Class<? extends java.lang.Throwable> expectedThrowableClass, java.lang.String expectedErrorMessage) throws java.lang.ReflectiveOperationExceptionTests that no instances of an utility class are created.- Parameters:
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 called- Throws:
java.lang.AssertionError- if the target class is instantiated or a different throwable class is receivedjava.lang.ReflectiveOperationException- in case of errors getting constructor metadata or instantiating the private constructor
-
assertException
public static void assertException(java.lang.Class<? extends java.lang.Throwable> expectedThrowable, java.lang.Throwable throwable)A utility method to assert that a given throwable matches the expected class.- Parameters:
expectedThrowable- the expected throwable classthrowable- the throwable to be validated
-
assertException
public static void assertException(java.lang.Class<? extends java.lang.Throwable> expectedThrowable, java.lang.String expectedMessage, java.lang.Throwable throwable)A utility method to assert that a given throwable matches the expected class and message.- Parameters:
expectedThrowable- the expected throwable classexpectedMessage- the expected message (if applicable)throwable- the throwable to be validated
-
assertException
public static void assertException(java.lang.Class<? extends java.lang.Throwable> expectedThrowable, java.lang.String expectedMessage, java.lang.Class<? extends java.lang.Throwable> expectedCause, java.lang.Throwable throwable)A utility method to assert that a given throwable matches the expected class, cause and message.- Parameters:
expectedThrowable- the expected throwable classexpectedMessage- the expected message (if applicable)expectedCause- the expected throwable cause class (if applicable)throwable- the throwable to be validated
-
assertException
public static void assertException(java.lang.Class<? extends java.lang.Throwable> expectedThrowable, java.util.function.Supplier<?> supplier)A utility method to assert the expected exception thrown by a supplying function.Example of usage:
assertException(IllegalArgumentException.class, () -> TestSubject.testMethod("param1"))- Parameters:
expectedThrowable- the expected throwable classsupplier- the supplying function that throws an exception to be validated
-
assertException
public static void assertException(java.lang.Class<? extends java.lang.Throwable> expectedThrowable, java.lang.String expectedMessage, java.util.function.Supplier<?> supplier)A utility method to assert the expected exception and message thrown by a supplying function.Example of usage:
assertException(IllegalArgumentException.class, "Invalid argument: param1", () -> TestSubject.testMethod("param1"))- Parameters:
expectedThrowable- the expected throwable classexpectedMessage- the expected message (if applicable)supplier- the supplying function that throws an exception to be validated
-
assertException
public static void assertException(java.lang.Class<? extends java.lang.Throwable> expectedThrowable, java.lang.String expectedMessage, java.lang.Class<? extends java.lang.Throwable> expectedCause, java.util.function.Supplier<?> supplier)A utility method to assert the expected exception, message and cause thrown by a supplying function.Example of usage:
assertException(MyException.class, "Unable to process the request", IllegalStateException.class() -> TestSubject.process("request1"))- Parameters:
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 validated
-
assertException
public static void assertException(java.lang.Class<? extends java.lang.Throwable> expectedThrowable, java.lang.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'srun()method).Example of usage:
assertException(IllegalStateException.class, () -> thread.start())- Parameters:
expectedThrowable- the expected throwable classrunnable- the runnable that produces an exception to be * validated
-
assertException
public static void assertException(java.lang.Class<? extends java.lang.Throwable> expectedThrowable, java.lang.String expectedMessage, java.lang.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'srun()method).Example of usage:
assertException(IllegalStateException.class, "Already started", () -> thread.start())- Parameters:
expectedThrowable- the expected throwable classexpectedMessage- the expected message (if applicable)runnable- the runnable that produces an exception to be * validated
-
assertException
public static void assertException(java.lang.Class<? extends java.lang.Throwable> expectedThrowable, java.lang.String expectedMessage, java.lang.Class<? extends java.lang.Throwable> expectedCause, java.lang.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'srun()method).Example of usage:
assertException(MyException.class, "Already started", IllegalStateException.class() -> thread.start())- Parameters:
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 * validated
-
assertStringContains
public static void assertStringContains(java.lang.String testString, java.lang.String... expectedStrings)Tests that the given test string contains all of the expected strings.- Parameters:
testString- the string to be testedexpectedStrings- the strings to be expected inside thetestString
-
assertStringDoesNotContain
public static void assertStringDoesNotContain(java.lang.String testString, java.lang.String... expectedStrings)Tests that the given test string does not contain any of the expected strings.- Parameters:
testString- the string to be testedexpectedStrings- the strings not to be expected inside thetestString
-