Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven: use specific test-classes of project A in project B

I have projects A and B where B requires A. Inside project A I have a utility-class UC that should only be available for JUnit-tests and, therefore, resides in src/test/java of project A. As long as I write tests in A I have access to UC. However, if I run Maven and want it to execute the tests in B, I get compiler errors since UC is not accessible in B.

Obviously Eclipse includes all classes in all source folders when it compiles something (i.e., it knows about UC when I write tests in B), while Maven removes all test-classes in the final version of A.

My question is this: what do I need to do to have UC accessible in B when I run its tests with Maven?

Please understand that I'm new to Maven and I think that similar questions have been asked. However, I can't convert what is written there into my problem and fix it.

I hope it's clear what I'm trying to do...

like image 976
sjngm Avatar asked Feb 25 '11 11:02

sjngm


People also ask

How to run a specific test class in Maven?

The Maven surefire plugin provides a test parameter that we can use to specify test classes or methods we want to execute. If we want to execute a single test class, we can execute the command mvn test -Dtest=”TestClassName”.

Where in the Maven project we keep test classes?

Running Unit Tests With the Maven Surefire Plugin The test classes must be found from the src/test/java directory. The name of the test class must start or end with the string: Test.

How to run tests using Maven?

We can run our unit tests with Maven by using the command: mvn clean test. When we run this command at command prompt, we should see that the Maven Surefire Plugin runs our unit tests. We can now create a Maven project that compiles and runs unit tests which use JUnit 5.

Does Maven include test classes in jar?

You can produce a jar which will include your test classes and resources. To reuse this artifact in an other project, you must declare this dependency with type test-jar : <project>


2 Answers

After looking some more I finally found a solution:

http://www.waltercedric.com/java-j2ee-mainmenu-53/361-maven-build-system/1349-maven-reusing-test-classes-across-multi-modules-projects.html 1

I've seen this pattern occasionally on other questions, so I guess I just didn't understand it that way... Oh, well. *eyeroll*

1 That original link stopped working. I found it again on archive.org (don't mind the awkward layout).

like image 74
sjngm Avatar answered Nov 16 '22 00:11

sjngm


The maven-jar-plugin page - http://maven.apache.org/plugins/maven-jar-plugin/usage.html - mentions two ways. The simple approach is creating a test-jar artifact, and then refer to that. (snippets blatantly copied from official page)

<project>
  ...
  <build>
    <plugins>
      ...
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.4</version>
        <executions>
          <execution>
            <goals>
              <goal>test-jar</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      ...
    </plugins>
  </build>
  ...
</project>

and then refer to it with the test-jar type and test scope in the projects that need it:

<project>
  ...
  <dependencies>
    <dependency>
      <groupId>groupId</groupId>
      <artifactId>artifactId</artifactId>
      <type>test-jar</type>
      <version>version</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  ...
</project>

If you need to do this a lot you should most likely consider moving your test code to separate projects.

like image 24
Thorbjørn Ravn Andersen Avatar answered Nov 16 '22 01:11

Thorbjørn Ravn Andersen