Class TestUtils


  • public class TestUtils
    extends Object
    A bunch of helpful functions for unit tests.
    Author:
    Johannes Schindelin, Curtis Rueden
    • Constructor Detail

      • TestUtils

        public TestUtils()
    • 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 File pointing 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 name
        forClass - the class for context (to determine whether there's a target/ 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 name
        forClass - the class for context (to determine whether there's a target/ 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