Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use maven resources also as test resources

I have a maven project that loads an xslt file and executes the transformation along with other processing on the result. Normally when the user runs the application, the user provides the xslt file path to be loaded. But I include some default xslt files bundled inside my application that the user can use without loading any external xslt file. I do this by adding them to src/main/resources/xslt. My problem is that I want to run tests against those xslt files in testing phase. How can I achieve this? Should I copy the src/main/resources/xslt contents to target/somewhere and load these in my test classes code? Which plugin is used for that?

like image 469
Paralife Avatar asked Jun 04 '10 23:06

Paralife


People also ask

Can Maven be used for testing?

Maven is the latest build testing tool. It has several new features as compare to Ant, like dependency, etc. Maven is a project build or project management tool. It is used to check the compilation issues between framework components whenever multiple test engineer integrates their files into the same framework.

What is Maven resources plugin used for?

The Resources Plugin handles the copying of project resources to the output directory. There are two different kinds of resources: main resources and test resources.

What is Maven resource filtering?

Resource Filtering. You can use Maven to perform variable replacement on project resources. When resource filtering is activated, Maven will scan resources for property references surrounded by ${ and }.


2 Answers

My problem is that I want to run tests against those xslt files in testing phase. How can I achieve this?

There is nothing to do, target/classes is on the class path of tests. More precisely, the class path for tests is:

  • first target/test-classes
  • then target/classes
  • then dependencies

So resources from src/main/resources (which are copied into target/classes) are visible from tests.

like image 112
Pascal Thivent Avatar answered Sep 29 '22 01:09

Pascal Thivent


If you put a file foo.txt inside src/test/resources/, you can open this via:

// try-with-resource (Java 1.7)
try (InputStream is = getClass().getClassLoader().getResourceAsStream("foo.txt")) {
    // do something with is...
}

You can also take a look at the maven-resources-plugin.

like image 30
MrDrews Avatar answered Sep 29 '22 00:09

MrDrews