Class BasicTestUtils


public abstract class BasicTestUtils extends Shorts
Shared, marshall-free test utilities common to Juneau test modules.

Provides the stable pure-JDK/commons subset of the per-module TestUtils classes: exception-message assertions, equality helpers, stream/reader factories, locale/timezone toggles, and URL construction. Module-level TestUtils classes extend this base and add their own marshall-bound helpers (e.g. assertJson, json, validateXml).

This class lives in the juneau-test-utils module, which depends only on juneau-commons and JUnit, so it never introduces a juneau-marshall dependency onto its consumers.

  • Field Details

  • Constructor Details

  • Method Details

    • assertEqualsAll

      public static void assertEqualsAll(Object... values)
      Asserts that all the specified values are equal to the first value.
      Parameters:
      values - The values to compare. Element 0 is the expected value.
    • assertNotEqualsAny

      public static void assertNotEqualsAny(Object actual, Object... values)
      Asserts that the specified actual value does not equal any of the specified values.
      Parameters:
      actual - The actual value. Must not be null.
      values - The values that must not match the actual value.
    • assertThrowsWithMessage

      public static <T extends Throwable> T assertThrowsWithMessage(Class<T> expectedType, List<String> expectedSubstrings, org.junit.jupiter.api.function.Executable executable)
      Asserts that the specified executable throws an exception whose chained messages contain all the specified substrings.
      Type Parameters:
      T - The expected exception type.
      Parameters:
      expectedType - The expected exception type.
      expectedSubstrings - The substrings expected in the exception message chain.
      executable - The code expected to throw.
      Returns:
      The thrown exception.
    • assertThrowsWithMessage

      public static <T extends Throwable> T assertThrowsWithMessage(Class<T> expectedType, String expectedSubstring, org.junit.jupiter.api.function.Executable executable)
      Asserts that the specified executable throws an exception whose chained messages contain the specified substring.
      Type Parameters:
      T - The expected exception type.
      Parameters:
      expectedType - The expected exception type.
      expectedSubstring - The substring expected in the exception message chain.
      executable - The code expected to throw.
      Returns:
      The thrown exception.
    • getMessages

      protected static String getMessages(Throwable t)
      Collects the newline-joined message chain of the specified throwable and all its causes.
      Parameters:
      t - The throwable.
      Returns:
      The joined message chain.
    • inputStream

      public static final ByteArrayInputStream inputStream(String in)
      Creates an input stream from the specified string.
      Parameters:
      in - The contents of the stream.
      Returns:
      A new input stream.
    • reader

      public static final StringReader reader(String in)
      Creates a reader from the specified string.
      Parameters:
      in - The contents of the reader.
      Returns:
      A new reader.
    • setLocale

      public static final void setLocale(Locale v)
      Temporarily sets the default system locale to the specified locale.
      Parameters:
      v - The new default locale.
    • setTimeZone

      public static final void setTimeZone(String v)
      Temporarily sets the default system timezone to the specified timezone ID.
      Parameters:
      v - The new default timezone ID.
    • unsetLocale

      public static final void unsetLocale()
      Restores the default system locale saved by setLocale(Locale).
    • unsetTimeZone

      public static final void unsetTimeZone()
      Restores the default system timezone saved by setTimeZone(String).
    • url

      public static URL url(String value)
      Constructs a URL object from a string.
      Parameters:
      value - The URL string.
      Returns:
      A new URL object.
    • await

      public static boolean await(BooleanSupplier condition, Duration timeout) throws InterruptedException
      Polls a condition until it holds or the timeout elapses.

      For tests that wait on state with no signal to block on, such as a file another process writes. Use this instead of a hand-rolled sleep loop. The condition is checked once more after the last sleep, so one that comes true right at the deadline still counts.

      Parameters:
      condition - The condition to wait for. Must not be null.
      timeout - How long to wait. Must not be null.
      Returns:
      true once the condition holds, or false if it still does not hold after the timeout.
      Throws:
      InterruptedException - If the thread is interrupted while waiting.
    • awaitFile

      public static boolean awaitFile(Path path, Duration timeout) throws InterruptedException
      Waits until a file exists or the timeout elapses.
      Parameters:
      path - The file to wait for. Must not be null.
      timeout - How long to wait. Must not be null.
      Returns:
      true once the file exists, or false if it still does not exist after the timeout.
      Throws:
      InterruptedException - If the thread is interrupted while waiting.
      See Also:
    • ea

      public static <T> T[] ea(Class<T> t)
      Returns an empty array of the specified type.
      Type Parameters:
      T - The component type.
      Parameters:
      t - The component type.
      Returns:
      A new empty array of the specified type.