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.
What am I doing wrong?
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”.
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 “.
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 “.
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.
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.
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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With