public class TestResource extends Object
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").
| Constructor and Description |
|---|
TestResource(String resourcePath) |
| Modifier and Type | Method and Description |
|---|---|
String |
contents()
Gets the contents of the given resources as string.
|
File |
file()
Gets the file associated to the resource.
|
String |
filePath()
Gets the associated file path.
|
String |
rawContents()
Gets all the contents of the resource without replacing OS specific new lines characters.
|
public TestResource(String resourcePath)
public File file()
This is handy when you need to take some file operations or need further information about the file.
public String filePath()
This is the same as file().getPath().
This handy when you need the file path, and avoid previously mentioned boilerplate code.
public String contents() throws IOException
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().
IOException - if there is some problem reading associated resource contents.public String rawContents() throws IOException
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().
IOException - if there is some problem reading associated resource contents.Copyright © 2024. All rights reserved.