Class BasicTestUtils
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 Summary
FieldsModifier and TypeFieldDescriptionstatic final ThreadLocal<Locale>Holds the previous default locale while a test overrides it viasetLocale(Locale). -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidassertEqualsAll(Object... values) Asserts that all the specified values are equal to the first value.static voidassertNotEqualsAny(Object actual, Object... values) Asserts that the specified actual value does not equal any of the specified values.static <T extends Throwable>
TassertThrowsWithMessage(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.static <T extends Throwable>
TassertThrowsWithMessage(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.static booleanawait(BooleanSupplier condition, Duration timeout) Polls a condition until it holds or the timeout elapses.static booleanWaits until a file exists or the timeout elapses.static <T> T[]Returns an empty array of the specified type.protected static StringCollects the newline-joined message chain of the specified throwable and all its causes.static final ByteArrayInputStreaminputStream(String in) Creates an input stream from the specified string.static final StringReaderCreates a reader from the specified string.static final voidTemporarily sets the default system locale to the specified locale.static final voidTemporarily sets the default system timezone to the specified timezone ID.static final voidRestores the default system locale saved bysetLocale(Locale).static final voidRestores the default system timezone saved bysetTimeZone(String).static URLConstructs aURLobject from a string.Methods inherited from class org.apache.juneau.commons.utils.Shorts
a, a2, aa, and, any, ao, at, b, bin, booleans, brex, brex, brex, brex, brex, brex, brex, btw, bytes, cast, cex, chars, chk, chknn, cmp, cn, cns, cnsq, co, cp, cp, cp, cp, cp, cp, cp, cp, cp, cp, cp, cp, cp, cp, cp, cp, cp, cp, cp, doubles, ein, ein, eq, eq, eq, eqa, eqic, eqic, ew, exex, exex, exex, f, fc, floats, fnb, fne, fs, gst, gt, gtc, gte, h, hs, iaex, iaex, iaex, ib, id, ie, ie, ie, ie, im, im, im, im, im, im, im, im, im, im, im, inb, ine, ine, ine, ine, ints, ioex, ioex, ioex, isex, isex, isex, ist, jn, l, lb, lc, lcr, len, ll, lm, longs, los, lt, lte, m, m, m, m, m, m, m, m, m, m, m, mb, mb, mf, mfs, n, n, na, neq, neq, neqic, nie, nie, nie, nie, nm, nm1, nn, not, o, o, oe, oo, or, quiet, r, req, reqcat, reqnb, reqnn, reqnn, reqnn, reqnn, reqnn, reqnns, reqnns, reqt, reqt, rex, rex, rex, rnn, s, sa, sa, sa, sb, sb, shorts, ss, st, stb, sw, sz, t, tl, tr, ts, u, u, u, u, uc, ucr, uoex, uoex, uoex, uoex, uoroex
-
Field Details
-
SYSTEM_LOCALE
Holds the previous default locale while a test overrides it viasetLocale(Locale).
-
-
Constructor Details
-
BasicTestUtils
public BasicTestUtils()
-
-
Method Details
-
assertEqualsAll
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
Asserts that the specified actual value does not equal any of the specified values.- Parameters:
actual- The actual value. Must not benull .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
Collects the newline-joined message chain of the specified throwable and all its causes.- Parameters:
t- The throwable.- Returns:
- The joined message chain.
-
inputStream
Creates an input stream from the specified string.- Parameters:
in- The contents of the stream.- Returns:
- A new input stream.
-
reader
Creates a reader from the specified string.- Parameters:
in- The contents of the reader.- Returns:
- A new reader.
-
setLocale
Temporarily sets the default system locale to the specified locale.- Parameters:
v- The new default locale.
-
setTimeZone
Temporarily sets the default system timezone to the specified timezone ID.- Parameters:
v- The new default timezone ID.
-
unsetLocale
Restores the default system locale saved bysetLocale(Locale). -
unsetTimeZone
Restores the default system timezone saved bysetTimeZone(String). -
url
Constructs aURLobject from a string.- Parameters:
value- The URL string.- Returns:
- A new
URLobject.
-
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 benull .timeout- How long to wait. Must not benull .- Returns:
true once the condition holds, orfalse if it still does not hold after the timeout.- Throws:
InterruptedException- If the thread is interrupted while waiting.
-
awaitFile
Waits until a file exists or the timeout elapses.- Parameters:
path- The file to wait for. Must not benull .timeout- How long to wait. Must not benull .- Returns:
true once the file exists, orfalse if it still does not exist after the timeout.- Throws:
InterruptedException- If the thread is interrupted while waiting.- See Also:
-
ea
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.
-