Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use groovy to load a test resource from my project structure

Tags:

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 ?

like image 884
Phoenix Avatar asked Oct 27 '11 22:10

Phoenix


People also ask

How do I run a groovy test?

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'.

How do you read a resource file in Junit?

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.

What is resource file in java?

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.


1 Answers

If it's in your classpath, then this should work:

String xmlString = this.getClass().getResource( '/datatest/a.xml' ).text 
like image 57
tim_yates Avatar answered Oct 15 '22 23:10

tim_yates