JAVA - can't import src/test/java to src/main/java

11,464

Solution 1

You're confusing two different concepts here, source folders and packages.

A source folder is just that, a folder that contains sources. In most modern projects, you will find two such source folders, one for production code, the other for test code. In your example (standard Maven directory layout, that's src/main/java and src/test/java, respectively).

A package is a way of organizing classes, which is usually reflected in your source also. By convention, test and production code use the same package, e.g. a production class com.mypackage.Foo would usually be tested by a class named com.mypackage.FooTest . By convention, these package hierarchies also map to directory hierarchies in your sources. Note: while in 99.99999% of cases, you should stick with this convention for sanity purposes, it's not actually a requirement. You can organize your packages independent of the file system arangement (on the source side).

Anyway, in a standard setup, your directory layout may look like this:

src/main/java/com/mycompany/mypackage/SomeClass.java
src/test/java/com/mycompany/mypackage/SomeClassTest.java

So production and test class share a package, but live in different source folders. This setup is very powerful, because it lets your build tool export only your production code to the final output, while giving your test code package-level access to the code it's testing. Test and production code are compiled independently, the production code is unaware of the test code, which is how it should be.

In your situation, it seems you have production code in the test folder. To change that, moving the code to a different package is not the solution. Instead, you want to move the code to the same package, but in the production folder. If you're using IntelliJ, the move dialog allows you to select a different source root, which is exactly what you want, other IDEs will over similar features.

Solution 2

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

Solution 3

I can't import packages from "src/test/java" to "src/main/java"

This is basically how Maven's model of software works. From Maven's perspective, the codebase of a project has two parts corresponding to the "main" and "test" sub trees.

  • The "main" subtree contains the software that you intend to deploy and run in production, or ship to customers, or whatever.

  • The "test" subtree contains the software whose sole purpose is testing the stuff in the main tree.

Code in the "test" subtree typically depends on the "main" tree, but code in the "main" tree should not1 depend on code in the "test" tree. Maven does not put the "test" code onto the classpath for compiling or running the "main" code, because that is likely to lead to accidental code dependencies of production code on test code. Which would be a bad thing.

1 - I would like to say cannot, but there might be some obscure way to get Maven to allow such dependencies. Either way, it is "wrong".


So ...

I have created an E2E testing project, initially, all the java classes were in 'src/test/java",

There are three possibilities here:

  • If this "testing" project consists of unit tests, then they should not be in a separate project. Unit tests belong in the same project as the code that they are testing. It should all be in that project's "test" tree.

  • If this "testing" project consists of system / functional tests for large-scale components implemented by other packages, then (probably) all of the code in the "testing" project belongs in the project's "main" tree. The "test" tree would only contain unit tests for the functional test framework ... and only if that made any sense for your project.

  • If this "testing" project comprises helper methods and things that you want to reuse in the unit tests or functional tests for other projects, then they should be in the "main" tree. To use them in the "test" tree of another project, you would add a dependency with scope "test" for the "testing" project in the other project.


But to sum it up, you shouldn't be moving test helper classes from "test" to "main". Leave them in the "test" tree (if this is unit testing) or restructure the projects as above.

The Junit (or whatever) test runners will only try to run tests that have appropriate class names, method annotations or whatever. The other (test dependent) classes in the "test" tree are fine where they are.

Solution 4

I faced the same however I got the issue resolved. In my case the pom dependancy had scope as test so I removed it and things started to work.

Share:
11,464
Nir Tal
Author by

Nir Tal

QA Team Leader

Updated on June 25, 2022

Comments

  • Nir Tal
    Nir Tal almost 2 years

    I have created an E2E testing project, initially, all the java classes were in 'src/test/java", but now I want to move all the files that do not contain tests to the "src/main/java" folder, the problem is after I do it:
    I can't import packages from "src/test/java" to "src/main/java" (but can do the opposite). like the packages do not exist at all for import.

    Cleaned the project and tried to run via IDE and via maven and it does not change the result.

    enter image description here

    What am I doing wrong?

  • Stephen C
    Stephen C over 2 years
    Well yea ... but that sounds like the dependency was accidental. It is likely to be more difficult for a deliberate dependency of "main" code on "test" code.
  • Stephen C
    Stephen C about 2 years
    Warning. If you do this, you may up with a Maven project that won't build / run unit tests outside of your IDE. That's a bad thing.