Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 335
Nir Tal Avatar asked Nov 29 '18 01:11

Nir Tal


People also ask

Is src/main/Java and SRC/test/Java already a source folder in Maven?

At times when you create a maven webapp project, you will not be seeing src/main/java and src/test/java as a source folder. When you even try to create it manually you will be getting the error like “The folder is already a source folder”.

Does samplewebapp have src/main/Java and SRC/test/Java folders in it?

Now we can that the SampleWebApp Project doesn’t have src/main/java and src/test/java folders in it. It has only the src/main/resource folder. Step 4: Even when We try to create a new Source folder We will an error like ” The Folder is already a source folder “.

Does Helloworld have src/main/Java and SRC/test/Java folders in it?

Now we can that the HelloWorld Project doesn’t have src/main/java and src/test/java folders in it. It has only the src/main/resource folder. Step 4 : Even when you try to create a new Source folder you will be getting the error like ” The Folder is already a source folder “.

How do I import test classes into eclipse?

You just need to import them. Eclipse should help you with this: if you try to use the class names, it'll suggest where to import from. The test classes are a separate package, so the classes will need to be declared public. Show activity on this post. Absolutely, one can do it.. "contains test sources" from No to Yes and save it.


4 Answers

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.

like image 50
Sean Patrick Floyd Avatar answered Oct 09 '22 01:10

Sean Patrick Floyd


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

like image 24
Rahul Barahate Avatar answered Oct 09 '22 02:10

Rahul Barahate


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.

like image 23
Stephen C Avatar answered Oct 09 '22 01:10

Stephen C


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.

like image 41
user8981221 Avatar answered Oct 09 '22 00:10

user8981221