I want to use the default maven directories for my junit tests, so:
- in Eclipse I created a
test/java
folder.
- I created a junit class in there with a
package com.xxx.xxx.tracker.utils.test;
But eclipse is giving me an error. It wants to change the package name to
package test.java.com.xxx.xxx.tracker.utils.test;
Or eclipse wants me to move it to a folder that corresponds to com/xxx/xxx/tracker/utils/test
Here is the exact message from eclipse:
- The declared package "com.xxx.xxx.tracker.utils.test" does not match the expected package "test.java.com.xxx.xxx.tracker.utils.test"
- 2 quick fixes available
- Move xxxTest.java to 'package com.xxx.xxx.tracker.utils.test'
- Change package declaration to 'test.java.com.xxx.xxx.tracker.utils.test'
I want to keep the junit in the test/java folder to keep it out of the build and follow convention, but it does not make sense to have test.java
in my package name.
Is there a way to keep the package name the way I want and also have it in the test/java folder?
EDIT: Is it because I have projects in my 'src' folder? For example:
src/
com.xxx.xxx.tracker.utils.Xxxx.java
test/
java/
com.xxx.xxx.tracker.utils.XxxxTest.java
This project did not start out with Maven. It was converted by someone else.
EDIT 2: More fuel for the fire:
Here is something from my pom.xml (which I did not set up).
<build>
<sourceDirectory>/home/me/workspace/proj_PARENT/proj_Classes/src</sourceDirectory>
<testSourceDirectory>/home/me/workspace/proj_PARENT/proj_Classes/src/test/java</testSourceDirectory>
I do have the eclipse m2e plugin.
You have a problem with you Structure and your classpath. With your descripbed layout the folder src is the root of your sources. But it contains the tests, which should be in a subfolder. In your case you have nested source folder. This MUST lead into compile problems, because a *.java file will be present in two packages. One of the packages is invalid.
src/
com.xxx.xxx.tracker.utils.Xxxx.java
test/
java/
com.xxx.xxx.tracker.utils.XxxxTest.java
Now you should create two src-folder.
src/
main/
java/
com.xxx.xxx.tracker.utils.Xxxx.java
test/
java/
com.xxx.xxx.tracker.utils.XxxxTest.java
A maven project following the standard Maven directory layout in eclipse should have the following source folders in its build path (snippet from the .classpath file)
<classpathentry kind="src" output="target/classes" path="src/main/java"/>
<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
Your eclipse project explorer should look like this :
If you're using the Eclipse m2e plugin, creating the src/main/java and src/test/java folders on your projects will result in them automatically getting added to the build path.
Update :
In your questions update, you're showing that you are nesting your testSourceDirectory in the sourceDirectory.
Most IDEs will warn you against doing that and will most likely lead to issues.
Eclipse can deal with it by excluding the test part from the source folder, but every time you'll try to update your project config through the m2e plugin, you'll again see the nested exception msg.
If you really want this kind of setup you'll need to configure the build path accordingly (right-click project - Build path - Configure build path).
Try adding a new folder test/java
to your project and configuring it as a source folder in Project > Properties > Java Build Path
. Look at the Source
tab.
Your directory layout should be:
project/
-> src/
-> test/
Using .test
as a suffix for test packages might not be necessary and will spare you an import statement when testing x.y.z.MyClass
in x.y.z.MyClassTest
.
Here is exactly what I did.
- Create new source directories:
src/test/java
and src/main/java
.
- Take all my current com.xxx.xxx.trackar* packages, select all, right click
Refactor > Move
and I moved them to src/main/java
- Create my new junit test in
src/test/java
using a package name w/o the .test suffix.
- Edit the pom.xml to conform to new source directories.
- Right click project
Maven > update Project
.
Project > Properties > Java Build Path
see Source
tab. Make sure src/test/java
and src/main/java
are in there and src
is not in there. If it is not correct, go back and update the pom.xml again (and view effective pom)...
- Close and open eclipse again.
- Package name error is gone!!!!!! Yay!
Thank you all for your help.
PS: I have lost revision control history, but it's because I'm using a legacy system.