I want to load a resource which is located inside of the /datatest folder in my project workspace. So, the whole file will be /datatest/a.xml .. How do I get the absolute path of this resource that I need in unit tests, since the unit test could be run on any machine I do not want to hardcode it but get it instead from using the classloader ? How do I do it ?
Test a Groovy applicationPress Ctrl+Shift+T and select Create New Test. In the dialog that opens, specify your test settings and click OK. Open the test in the editor, add code and press Ctrl+Shift+F10 or right-click the test class and from the context menu select Run 'test name'.
File class to read the /src/test/resources directory by calling the getAbsolutePath() method: String path = "src/test/resources"; File file = new File(path); String absolutePath = file. getAbsolutePath(); System. out.
In the Java programming language a resource is a piece of data that can be accessed by the code of an application. An application can access its resources through uniform resource locators, like web resources, but the resources are usually contained within the JAR file(s) of the application.
If it's in your classpath, then this should work:
String xmlString = this.getClass().getResource( '/datatest/a.xml' ).text
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With