Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven - Add directory to classpath while executing tests

Tags:

maven-2

People also ask

Does Maven add to classpath?

Maven Archiver can add the classpath of your project to the manifest.

How do I exclude test cases in Maven?

To skip running the tests for a particular project, set the skipTests property to true. You can also skip the tests via the command line by executing the following command: mvn install -DskipTests.

How do I create a resource SRC test?

Right click on maven project --->Click on Build Path ----->Click on New Source Folder. New source folder window will open, give the name to your folder example - src/test/source. click on Finish.


You can also add new test resource folders.

<build>
    <testResources>
        <testResource>
            <directory>${project.basedir}/src/test/resources</directory>
        </testResource>
        <testResource>
            <directory>${project.basedir}/src/test/something_else</directory>
        </testResource>
    </testResources>
</build>

The first path, src/test/resources, is the default. Assuming you still want the default path to be used, make sure it's included. (The testResources tag overwrites your defaults, so if you don't include the default path explicitly, it will stop being used.)


You can use the build-helper-maven-plugin to specify additional test-resource directories as follows. Using the configuration below, the contents of the test-resources directory will be copied to the target/test-classes directory during the generate-test-sources phase:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>build-helper-maven-plugin</artifactId>
  <version>1.12</version>
  <executions>
    <execution>
      <id>add-test-resource</id>
      <phase>generate-test-sources</phase>
      <goals>
        <goal>add-test-resource</goal>
      </goals>
      <configuration>
        <resources>
          <resource>
            <directory>path/to/additional/test/resources</directory>
            <excludes>
              <exclude>**/folder-to-exclude/**</exclude>
            </excludes>
          </resource>
        </resources>
      </configuration>
    </execution> 
  </executions>
</plugin>

If you just want to put your property files someplace on disk and don't want to copy those property files to target/test-classes during the build, you can do it this way

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <configuration>
    <additionalClasspathElements>
      <additionalClasspathElement>/add/this/to/path</additionalClasspathElement>
    </additionalClasspathElements>
  </configuration>
</plugin>

Why not just use test/resources and place your properties in the classpath from that point. They'll only be there for the test phase.


If you have multiple resource environment you can use maven profile and put your various resources according to the profile you are testing.

test/resources/uat
test/resources/prod
test/resources/dev

But usualy if you need that you are making integration test then you don't need the build-helper-maven-plugin.


The maven-resources-plugin has a copy-resources goal that will allow you to copy resources. For example:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.6</version>
    <executions>
      <execution>
        <id>additional-resources</id>
        <phase>process-test-resources</phase>
        <goals>
          <goal>copy-resources</goal>
        </goals>
        <configuration>
          <outputDirectory>${project.build.testOutputDirectory}</outputDirectory>
          <resources>
            <resource>
              <directory>${project.basedir}/conf</directory>
            </resource>
          </resources>
        </configuration>
      </execution>
    </executions>
  </plugin>

This will copy the contents of the conf folder in the base of your project to the target/test-classes folder (unless you modified project.build.testOutputDirectory) which will be added to the classpath during your unit tests.