Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reuse classes from /src/test/java in different project?

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?

like image 398
membersound Avatar asked May 12 '26 13:05

membersound


1 Answers

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>
like image 164
Amer Qarabsa Avatar answered May 14 '26 03:05

Amer Qarabsa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!