2

I'm preparing a test env. for my project.
I have a bunch of maven projects and some of the projects are using common configuration for spring beans test (test-context.xml). All the tests works when I have this configuration in src\test\resources of every project. When I want to move that configuration to a separate project (project-test) to eliminate duplicates in every project I get an Exception:

Caused by: java.io.FileNotFoundException: class path resource [test-context.xml] cannot be opened because it does not exist
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:157)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
    ... 39 more
  • I've added a maven dependency to project-tests in the project with tests
  • the test-context.xml doesn't go to the target directory
  • when I put my resources in src/main/resources instead of src/test/resources it works

What is the way to add the test resource from another project so it's visible from a TestNG test?
Is moving it to src/main/resources the best way in this situation?

aurban
  • 161
  • 4
  • 16
  • possible duplicate of [Sharing Test code in Maven](http://stackoverflow.com/questions/174560/sharing-test-code-in-maven) – artbristol Sep 26 '12 at 10:34

2 Answers2

3

You can't simply have a dependency on project-tests and expect the resource from src/test/resources to come through. You will need to build a test JAR from your project-tests project. Luckily for you this is quite simple and has been explained very well in another SO thread Sharing Test code in Maven

The basic idea is to generate a test jar file of project-tests with a classifier and then use that as a dependency in your project. This approach is best practice for what you are trying to do.

Community
  • 1
  • 1
ramsinb
  • 1,957
  • 12
  • 16
1

Ramsinb solution is correct. But if you deal only with resources, prefer an assembly which is better designed for this, and share a zip.

Zip Project :

pom.xml

<groupId>com.mycompany</groupId>
<artifactId>cfg_dev</artifactId>
<version>1.1.0</version>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <id>cfg-main-resources</id>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <phase>package</phase>
                    <configuration>
                        <descriptors>
                            <descriptor>/src/main/assembly/resources.xml</descriptor>
                        </descriptors>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Assembly descriptor : It will produce this artifact : cfg_dev-1.1.0-resources.zip Please, note that

  1. this is a zip archive
  2. the "classifier" is resources (like assembly name)

    resources zip false src/main/resources

Main Project :

pom.xml

Please, note that

  1. this depends on a zip archive
  2. the dependency "classifier" is resources (like previous assembly name)

    <!-- Unit test dependency -->
    <dependency>
        <groupId>com.mycompany</groupId>
        <artifactId>cfg_dev</artifactId>
        <version>${project.version}</version>
        <classifier>resources</classifier>
        <type>zip</type>
        <scope>test</scope>
    </dependency>
    
    ...
    

    src/test/resources true ${project.build.directory}/test-resources true

    <plugins>
    
    
        <!-- Unzip shared resources -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>unpack-cfg-test-resources</id>
                    <goals>
                        <goal>unpack-dependencies</goal>
                    </goals>
                    <phase>generate-test-resources</phase>
                    <configuration>
                        <outputDirectory>${project.build.directory}/test-resources</outputDirectory>
                        <includeArtifacIds>cfg_dev</includeArtifacIds>
                        <includeGroupIds>${project.groupId}</includeGroupIds>
                        <excludeTransitive>true</excludeTransitive>
                        <excludeTypes>pom</excludeTypes>
                        <scope>test</scope>
                    </configuration>
                </execution>
            </executions>
        </plugin>
      </plugins>
    

Community
  • 1
  • 1
Jean-Rémy Revy
  • 5,431
  • 3
  • 36
  • 64