4

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?

5
  • 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. Commented Jun 24, 2015 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. Commented Jun 24, 2015 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. Commented Jun 24, 2015 at 11:33
  • Specifically, check out flicken's answer in the duplicate question: stackoverflow.com/a/174670/423991, that should solve your problems. Commented Jun 24, 2015 at 11:38
  • Thanks for point me to the previously answered question. Commented Jun 24, 2015 at 11:43

1 Answer 1

3

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

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

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>' 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.