Class TestUtils
java.lang.Object
test.de.iip_ecosphere.platform.services.environment.metricsProvider.utils.TestUtils
Class to avoid repetition of code in testing environment.
This class extracts a JsonObject from a resource and has some timed methods.
This class extracts a JsonObject from a resource and has some timed methods.
- Author:
- Miguel Gomez
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends Throwable>
TassertThrows(Class<T> expectedThrowable, ThrowingRunnable runnable) Asserts thatrunnablethrows an exception of typeexpectedThrowablewhen executed.static <T extends Throwable>
TassertThrows(String message, Class<T> expectedThrowable, ThrowingRunnable runnable) Asserts thatrunnablethrows an exception of typeexpectedThrowablewhen executed.private static StringbuildPrefix(String message) Builds a message prefix.private static booleanequalsRegardingNull(Object expected, Object actual) Returns whether expected and actual are considered equal regarding null.(package private) static StringFormats a message with given expected and actual value.private static StringformatClass(Class<?> value) Formats the given class name.private static StringformatClassAndValue(Object value, String valueString) Formats a class name including value.private static booleanReturns whether expected and actual are considered equal.static voidRuns/waits for one second.static StringRuns/waits for one second and returnsDATA.static de.iip_ecosphere.platform.support.json.JsonObjectreadJsonFromResources(String folder, String filename) Extracts a JsonObject from a resource.
The resource will be nested in a directory nested under the directoryjsonsamplesin the resource directory.static voidRuns/waits for three seconds.static StringRuns/waits for three seconds and returnsDATA.static voidRuns/waits for two seconds.static StringRuns/waits for two seconds and returnsDATA.
-
Field Details
-
DATA
- See Also:
-
-
Constructor Details
-
TestUtils
public TestUtils()
-
-
Method Details
-
readJsonFromResources
public static de.iip_ecosphere.platform.support.json.JsonObject readJsonFromResources(String folder, String filename) throws IOException Extracts a JsonObject from a resource.
The resource will be nested in a directory nested under the directoryjsonsamplesin the resource directory.- Parameters:
folder- name of the directory where the resource is foundfilename- name of the file containing the resource- Returns:
- JSON representation of the resource
- Throws:
IOException- if the file cannot be found or an error occurs when opening the stream
-
oneSecondRunnable
public static void oneSecondRunnable()Runs/waits for one second. -
twoSecondRunnable
public static void twoSecondRunnable()Runs/waits for two seconds. -
threeSecondRunnable
public static void threeSecondRunnable()Runs/waits for three seconds. -
oneSecondSupplier
Runs/waits for one second and returnsDATA.- Returns:
DATA
-
twoSecondSupplier
Runs/waits for two seconds and returnsDATA.- Returns:
DATA
-
threeSecondSupplier
Runs/waits for three seconds and returnsDATA.- Returns:
DATA
-
assertThrows
public static <T extends Throwable> T assertThrows(Class<T> expectedThrowable, ThrowingRunnable runnable) Asserts thatrunnablethrows an exception of typeexpectedThrowablewhen executed. If it does, the exception object is returned. If it does not throw an exception, anAssertionErroris thrown. If it throws the wrong type of exception, anAssertionErroris thrown describing the mismatch; the exception that was actually thrown can be obtained by callingThrowable.getCause().- Type Parameters:
T- throwable type- Parameters:
expectedThrowable- the expected type of the exceptionrunnable- a function that is expected to throw an exception when executed- Returns:
- the exception thrown by
runnable
-
assertThrows
public static <T extends Throwable> T assertThrows(String message, Class<T> expectedThrowable, ThrowingRunnable runnable) Asserts thatrunnablethrows an exception of typeexpectedThrowablewhen executed. If it does, the exception object is returned. If it does not throw an exception, anAssertionErroris thrown. If it throws the wrong type of exception, anAssertionErroris thrown describing the mismatch; the exception that was actually thrown can be obtained by callingThrowable.getCause().- Type Parameters:
T- throwable type- Parameters:
message- the identifying message for theAssertionError(nullokay)expectedThrowable- the expected type of the exceptionrunnable- a function that is expected to throw an exception when executed- Returns:
- the exception thrown by
runnable
-
buildPrefix
Builds a message prefix.- Parameters:
message- the message- Returns:
- the message with prefix
-
formatClass
Formats the given class name.- Parameters:
value- the class- Returns:
- the formatted class name
-
format
Formats a message with given expected and actual value.- Parameters:
message- the messageexpected- the expected objectactual- the actual object- Returns:
- the message
-
formatClassAndValue
Formats a class name including value.- Parameters:
value- the class/object valuevalueString- the value- Returns:
- the formatted string
-
equalsRegardingNull
Returns whether expected and actual are considered equal regarding null.- Parameters:
expected- the expected valueactual- the actual value- Returns:
truefor equal,falseelse
-
isEquals
Returns whether expected and actual are considered equal.- Parameters:
expected- the expected valueactual- the actual value- Returns:
truefor equal,falseelse
-