I am writing a small test to read a CSV file (using testng). I found some code to do just that and some other lines to find the resource folder itself.
Now as I am re-using this in other scenarios, I thought that I could just create a util method instead of copy pasting:
public static String prepFilepath(Class c) {
URL url = c.getResource("");
String location = url.getPath();
String packageName = c.getPackage().getName().replace(".", "/");
int l = location.lastIndexOf(packageName);
return location.substring(0, l);
}
and it is called by my test like this:
@Test
public void testImport(){
File testFile = new File(ImportUtils.prepFilepath(this.getClass()), inputFileName);
//...
}
However I don't know if it is the correct way:
- Is it ok to pass a class reference as parameter? (I had never done that before for such a small purpose)
- Are there more elegant ways?
- Is it generic enough to justify the util?
Edit: I am using testng