0

i got a Java project which has multiple test directorys at resources.

\target\test-classes
                   |___conf
                   |      |_ items...
                   |___otherdata
                          |_ items...

I need to access \target\test-classes at it's root, so I can switch between directories since a testcase uses files from BOTH directories at once.

Tried:

File targetClassesDir = new File(getClass().getProtectionDomain().getCodeSource().getLocation().getPath());
File targetDir = targetClassesDir.getParentFile();

and

getClass().getResource("test-classes").getPath()

but counldn't access the root of test-classes yet.

I've constructed a pretty shitty workaround...:

tuple4TestsuiteContainer.add(new HeaderBodyPayloadTestcase(
                                    "conf\\" + xqueries[0],
                                    getClass().getResource("/").getPath(),
                                    "xquery-testdata\\" + file.substring(file.lastIndexOf(File.separatorChar) + 1),
                                    "xquery-testdata\\" +expectedFiles[0].substring(expectedFiles[0].lastIndexOf(File.separatorChar) + 1)
                            ));

and in the actual tester:

String xqueryFileContent = readFile(testDataBasePath + xQueryUnderTestPath, encoding);
Amit
  • 30,756
  • 6
  • 57
  • 88
Hendrik
  • 310
  • 6
  • 19

3 Answers3

1

I'm also not sure about your use case and project setup (Maven?), but in my opinion you shouldn't access any files in target/test-classes explicitly at all and load resources using the ClassLoader from resource folders.

How to get the path of src/test/resources directory in JUnit?

DoNuT
  • 463
  • 1
  • 7
  • 23
0

I am not sure whether, I was able to understand your problem correctly. If in java while running you are trying to change the directory, you can simply do something like this :

    public class Main {
            public static void main(String args[]) {
                    String[] s = { "c:\\some\\place\\cdexe.exe",
                    "c:\\start\\dir", "c:\\my\\batch\\file.bat", "arg1", "..." };
                    try {
                            java.lang.Runtime.getRuntime().exec(s);
                    } catch (java.io.IOException e) {
                            e.printStackTrace();
                    }
            } }

Let me know if this helps, would like to understand your problem more.

Amit
  • 30,756
  • 6
  • 57
  • 88
anshul Gupta
  • 1,182
  • 8
  • 17
0

I am not sure if I understand your problem correctly. I assume you need to get access to some data (resource). If the resource is copied to your target directory during the maven step, then you can get the path to this resource in your code (e.g. test) via something like

String resrouce = "data/bla/blub.txt";
String pathToResource = URLDecoder.decode(this.getClass().getResource(resource).getFile(), "UTF-8");
Christian Fries
  • 16,175
  • 10
  • 56
  • 67
  • My program has a "basedir" which should be test-classes then I get my testdata like "/conf/data1" and "/otherdata/data2" Your example is partly what I need – Hendrik Jun 07 '17 at 11:43