Class TestResource


  • public class TestResource
    extends Object
    Eases usage of test resources files (e.g.: files in src/test/resources).

    This internally uses java ClassLoader.getResource(String). For instance, if you want to reference a file in "src/test/resources/csvs/myCsv.csv" in a maven project, you will need to use new TestResource("csvs/myCsv.csv").

    Since:
    0.54
    • Constructor Detail

      • TestResource

        public TestResource​(String resourcePath)
    • Method Detail

      • file

        public File file()
        Gets the file associated to the resource.

        This is handy when you need to take some file operations or need further information about the file.

        Returns:
        the associated file.
      • filePath

        public String filePath()
        Gets the associated file path.

        This is the same as file().getPath().

        This handy when you need the file path, and avoid previously mentioned boilerplate code.

        Returns:
        the associated file path.
      • contents

        public String contents()
                        throws IOException
        Gets the contents of the given resources as string.

        Take into consideration that this uses UTF_8 encoding for reading the file and that file should be in general be a textual one (otherwise decoding it may fail).

        Note that this method is known to have some issues, check rawContents().

        Returns:
        text decoded contents of the file.
        Throws:
        IOException - if there is some problem reading associated resource contents.
      • rawContents

        public String rawContents()
                           throws IOException
        Gets all the contents of the resource without replacing OS specific new lines characters.

        Additionally, this method works even when resource is inside jar, when contents() doesn't.

        In version 2.0 we will replace contents() by this method, but for the time being keeping both as to avoid breaking existing users code that might be using contents().

        Returns:
        text contents of the resource.
        Throws:
        IOException - if there is some problem reading associated resource contents.
        Since:
        1.9