0

I have a class in a commons library that I placed into /src/test/java. Then I want to reuse that class in any project having the commons library as dependency.

But the file cannot be imported.

Common custom library:

/src/test/java/com/myproject/utils/TestfileReader.java

Implementation project:

<dependencies>
    <dependency>
        <groupId>com.myproject</groupId>
        <artifactId>my-commons</artifactId>
        <version>1.0.0</version>
    </dependency>

usage:

/src/test/java/com/myproject/itest/SomeTest.java

import com.myproject.utils.TestfileReader;

Result: the import cannot be resolved.

But if I copy that file to /src/main/java/... it can be found correctly. So my commons library seems to be fine in general.

Question: how can I make this file visible only to my tests, while keeping it in /src/test/java folder?

membersound
  • 66,525
  • 139
  • 452
  • 886
  • put it in a separate project, add a dependency to this project in each project where you want to use it in a test. – Stultuske Jan 04 '18 at 09:21
  • So I necessarily have to put that class under `/src/main/java`? – membersound Jan 04 '18 at 09:21
  • So, you added a pom entry for that dependency, but is there such lib actually? (at least in your local repository) – Tom Jan 04 '18 at 09:22
  • 1
    @Tom I think the problem is that the class is under the test classes, not under source – Stultuske Jan 04 '18 at 09:23
  • @Tom yes of course. As written if I move that file to `/src/main/java` inside the commons library, it is found correctly. – membersound Jan 04 '18 at 09:23
  • @membersound it seems quite unlogical that a same class, which is designed to test one specific class in project A can also be used to test a class in project B. if there is, there's something about code-reuse missing in the projects. So I'm not sure you can import classes under test – Stultuske Jan 04 '18 at 09:24
  • Possible duplicate of [Sharing Test code in Maven](https://stackoverflow.com/questions/174560/sharing-test-code-in-maven) – Tom Jan 04 '18 at 09:26
  • Then why is there no "scope" tag? Please read the dupe, it might solve the issue. – Tom Jan 04 '18 at 09:26
  • @Stultuske Well in this case it's a common filereader that reads files from a path (provided as parameter), and processes the file in a specific way. If I need that routine in multiple projects, I think it's totally fine to reuse that class. But as it is only suitable for testing, I tend to not include it in the main path... – membersound Jan 04 '18 at 09:27
  • @membersound so add it in a seperate (small) library, which you put as dependency and re-use in both projects' tests – Stultuske Jan 04 '18 at 09:34

4 Answers4

2

you can do this by specifying the type to test-jarscope.

<dependency>
    <groupId>com.myproject</groupId>
    <artifactId>my-commons</artifactId>
    <version>1.0.0</version>
    <type>test-jar</type>
    <scope>test</scope>
</dependency>
membersound
  • 66,525
  • 139
  • 452
  • 886
Amer Qarabsa
  • 5,678
  • 3
  • 16
  • 31
0

You can control whether your resources under your src/test folder will get shipped or not, e.g. via the maven-resource-plugin See here: http://maven.apache.org/plugins/maven-resources-plugin/testResources-mojo.html

An example here would copy all sources found under src/test/java into your final build.

<testResources>
  <testResource>
    <directory>${project.basedir}/src/test/java</directory>
  </testResource>
</testResources>
mrkernelpanic
  • 3,456
  • 3
  • 22
  • 46
0

You may do the following:

  • Create a jar file from the project that you want to share in different projects.
  • Use this jar file as dependencies by giving group id, artifact id and version.
  • Remember to add index of this jar file in your external dependencies.
Nikhil Pareek
  • 604
  • 8
  • 21
0

Absolutely, one can do it..

First You need to navigate to Build Path-->Configure Build Path--> Source tab

then in the Source tab search/check [your Project Name]/src/main/java and change

"contains test sources" from No to Yes and save it.

This will resolved issue of import packages from "src/test/java" to "src/main/java" successfully

jizhihaoSAMA
  • 10,685
  • 9
  • 18
  • 36