2

I have a module as part of a framework that provides common features to other modules. I have created a class (UtilitiesForTesting) that provides some utils to be used only in my tests, so it's stored in test section of the module.

The module has the following structure:

project
    -src
       -main
         -java
           -com
             -company
               -product
                 +Foo
       -test
         -java
           -com
             -company
               -product
                 +FooTest
               -utility
                 +UtilitiesForTesting

UtilitiesForTesting class is visible inside the project but when I compile it the resulting jar does not contains any test class.

Now I want to use the modules in other modules, because I need this common functionality, but also I want to use testing utilities.

I don't want to place UtilitiesForTesting in the /main section, since I don't want to allow the use of this class in production code, only in test.

I want to have just a module that provides production classes to other module and test util to the same module, but restricting using the test utilities in the /main section.

Is there any way to use a class of the test scope of a module in other module?

Ezequiel
  • 3,277
  • 1
  • 15
  • 27
  • You can always export it to an external library containing nothing but testtools, and just add a dependency to this library in both modules/applications/projects. – Stultuske Jun 24 '15 at 11:27
  • Yes, but in this way this module could be imported with ´compile´ scope by other modules and use it production code. I want to restrict the use only for testing. – Ezequiel Jun 24 '15 at 11:30
  • Then the *other* modules can import with actual test scope. This example *isn't* "test scope", which applies to imports; it's dealing with the test classpath. – chrylis -cautiouslyoptimistic- Jun 24 '15 at 11:33
  • Specifically, check out flicken's answer in the duplicate question: http://stackoverflow.com/a/174670/423991, that should solve your problems. – K Erlandsson Jun 24 '15 at 11:38
  • Thanks for point me to the previously answered question. – Ezequiel Jun 24 '15 at 11:43

1 Answers1

3

In your pom.xml, under maven-jar-plugin, add below goal.

`<goal>test-jar</goal&gt`

All the files under src/test/java are compiled and bundled into artifact-test.jar.

In another module, if u want to use this artifact-test jar, use below configuration in dependency tag.

 `<artifactId>artifactid</artifactId>
<type>test-jar</type>'
kswaughs
  • 2,623
  • 1
  • 13
  • 19