Hi i have a structure something like this.
parent.pom
|-alpha.pom
|-beta.pom
in beta i have a dependency towards alpha and in betas test i have a reference to a class in alpha's src/test/java
However it seems when I run the beta test it cannot find the file in alpha.
The class itself is implementing a file from src/main/java
, so it is something like FooBarFactoryForTest implements FooBarFactory
. And it cannot find FooBarFactoryForTest when i do new FooBarFactoryForTest()
in beta's test
Also the same structure works in tests in alpha.
Any ideas or suggestions?
That's because the test classes are not packaged by default in any project, let alone your alpha
project. It would probably work in your eclipse if you have turned on the workspace resolution feature.
To make it work with Maven
, consider creating a test jar containing only of test classes in alpha
and adding as a dependency in beta
.
Read: https://maven.apache.org/plugins/maven-jar-plugin/examples/create-test-jar.html
You can produce a jar which will include your test classes and resources.
<project> ... <build> <plugins> ... <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.6</version> <executions> <execution> <goals> <goal>test-jar</goal> </goals> </execution> </executions> </plugin> ... </plugins> </build> ... </project>
To reuse this artifact in an other project, you must declare this dependency with type test-jar :
<project> ... <dependencies> <dependency> <groupId>groupId</groupId> <artifactId>artifactId</artifactId> <type>test-jar</type> <version>version</version> <scope>test</scope> </dependency> </dependencies> ... </project>
Note: The downside of this solution is that you don't get the transitive test-scoped dependencies automatically. Maven only resolves the compile-time dependencies, so you'll have to add all the other required test-scoped dependencies by hand.