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 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's run() method).
    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's run() method).
    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.
    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.
    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's run() method).
    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.
    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.
    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.
    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.
    static void assertNoInstancesAllowed​(java.lang.Class<?> targetClass)
    Tests that no instances of an utility class are created.
    static void assertNoInstancesAllowed​(java.lang.Class<?> targetClass, java.lang.Class<? extends java.lang.Throwable> expectedThrowableClass)
    Tests that no instances of an utility class are created.
    static void assertNoInstancesAllowed​(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 void assertStringContains​(java.lang.String testString, java.lang.String... expectedStrings)
    Tests that the given test string contains all of the expected strings.
    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.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • assertNoInstancesAllowed

      public static void assertNoInstancesAllowed​(java.lang.Class<?> targetClass) throws java.lang.ReflectiveOperationException
      Tests 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 instantiated
      java.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.ReflectiveOperationException
      Tests that no instances of an utility class are created.
      Parameters:
      targetClass - the class to be tested
      expectedThrowableClass - 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 received
      java.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.ReflectiveOperationException
      Tests that no instances of an utility class are created.
      Parameters:
      targetClass - the class to be tested
      expectedThrowableClass - the expected throwable to be checked in case the constructor is called
      expectedErrorMessage - 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 received
      java.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 class
      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.Throwable throwable)
      A utility method to assert that a given throwable matches the expected class and message.
      Parameters:
      expectedThrowable - the expected throwable class
      expectedMessage - 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 class
      expectedMessage - 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 class
      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.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 class
      expectedMessage - 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 class
      expectedMessage - 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's run() method).

      Example of usage:

       assertException(IllegalStateException.class, () -> thread.start())
       
      Parameters:
      expectedThrowable - the expected throwable class
      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.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).

      Example of usage:

       
       assertException(IllegalStateException.class, "Already started",
               () -> thread.start())
       
       
      Parameters:
      expectedThrowable - the expected throwable class
      expectedMessage - 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's run() method).

      Example of usage:

       
       assertException(MyException.class, "Already started",
               IllegalStateException.class() -> thread.start())
       
       
      Parameters:
      expectedThrowable - the expected throwable class
      expectedMessage - 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 tested
      expectedStrings - the strings to be expected inside the testString
    • 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 tested
      expectedStrings - the strings not to be expected inside the testString