Class TestUtils
- java.lang.Object
-
- org.scijava.test.TestUtils
-
public class TestUtils extends Object
A bunch of helpful functions for unit tests.- Author:
- Johannes Schindelin, Curtis Rueden
-
-
Constructor Summary
Constructors Constructor Description TestUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static FilecreatePath(File parent, String path)Creates an empty file at the given path, creating intermediate directories as necessary.static FilecreateTemporaryDirectory(String prefix)Makes a temporary directory for use with unit tests.static FilecreateTemporaryDirectory(String prefix, Class<?> forClass)Makes a temporary directory for use with unit tests.static FilecreateTemporaryDirectory(String prefix, Class<?> forClass, String suffix)Makes a temporary directory for use with unit tests.static Class<?>getCallingClass(Class<?> excluding)Returns the class of the caller (excluding the specified class).static Map.Entry<Class<?>,String>getCallingCodeLocation(Class<?> excluding)Returns the class and the method/line number of the caller (excluding the specified class).
-
-
-
Method Detail
-
createPath
public static File createPath(File parent, String path) throws IOException
Creates an empty file at the given path, creating intermediate directories as necessary.- Parameters:
parent- The parent directory of the relative path.path- The forward-slash-separated path to create.- Returns:
- a
Filepointing at the newly created empty path. - Throws:
IOException- if the file cannot be created.
-
createTemporaryDirectory
public static File createTemporaryDirectory(String prefix) throws IOException
Makes a temporary directory for use with unit tests.When the unit test runs in a Maven context, the temporary directory will be created in the
target/directory corresponding to the calling class instead of/tmp/.- Parameters:
prefix- the prefix for the directory's name- Returns:
- the reference to the newly-created temporary directory
- Throws:
IOException
-
createTemporaryDirectory
public static File createTemporaryDirectory(String prefix, Class<?> forClass) throws IOException
Makes a temporary directory for use with unit tests.When the unit test runs in a Maven context, the temporary directory will be created in the corresponding
target/directory instead of/tmp/.- Parameters:
prefix- the prefix for the directory's nameforClass- the class for context (to determine whether there's atarget/directory)- Returns:
- the reference to the newly-created temporary directory
- Throws:
IOException
-
createTemporaryDirectory
public static File createTemporaryDirectory(String prefix, Class<?> forClass, String suffix) throws IOException
Makes a temporary directory for use with unit tests.When the unit test runs in a Maven context, the temporary directory will be created in the corresponding
target/directory instead of/tmp/.- Parameters:
prefix- the prefix for the directory's nameforClass- the class for context (to determine whether there's atarget/directory)suffix- the suffix for the directory's name- Returns:
- the reference to the newly-created temporary directory
- Throws:
IOException
-
getCallingClass
public static Class<?> getCallingClass(Class<?> excluding)
Returns the class of the caller (excluding the specified class).Sometimes it is convenient to determine the caller's context, e.g. to determine whether running in a maven-surefire-plugin context (in which case the location of the caller's class would end in
target/test-classes/).- Parameters:
excluding- the class to exclude (or null)- Returns:
- the class of the caller
-
getCallingCodeLocation
public static Map.Entry<Class<?>,String> getCallingCodeLocation(Class<?> excluding)
Returns the class and the method/line number of the caller (excluding the specified class).Sometimes it is convenient to determine the caller's context, e.g. to determine whether running in a maven-surefire-plugin context (in which case the location of the caller's class would end in
target/test-classes/).- Parameters:
excluding- the class to exclude (or null)- Returns:
- the class of the caller and the method and line number
-
-