• Post Reply Bookmark Topic Watch Topic
  • New Topic

Unit Tests and Eclipse Project structure: best practices  RSS feed

 
Geane Norm
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I usually create Eclipse Java project where there are 3 high level folders:

-src
-- mypackage
--- class1.java

-bin
--(compile files) class1.class

-lib
--library.jar

When it comes to Unit tests and Mocks, I am not sure what is the recommended approach. Should I create a separate test folder and include all my test files there? Can you please advise?

Thanks,

I.
 
Tim Cooke
Marshal
Posts: 4039
239
Clojure IntelliJ IDE Java
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, and welcome to the Ranch!

Under the src directory I usually organise my code as follows. This is how Maven likes to arrange things.

Taken from my Blog on creating a bare bones Java application with Maven.
 
Geane Norm
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brilliant, thank you so much! Do you need a separate package for your tests?

Thank you,

I.
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look at the above structure given by Tim, it is not only different package for testing, but the parent directories are different altogether.
When you create a maven project, the source goes under "src/main/java" and the test goes under "test/java"
 
Tim Cooke
Marshal
Posts: 4039
239
Clojure IntelliJ IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's handy to have your test classes in the same package as the class under test. I won't go into the full ins and outs of it now but having the ability to write package level methods in your class under test to be called from the test class is really useful at times.

In my example "src/main/java" and "src/test/java" are the source directories, and "com.trcooke.mvndemo" is the package.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tapas Chand wrote:If you look at the above structure given by Tim, it is not only different package for testing, but the parent directories are different altogether.
When you create a maven project, the source goes under "src/main/java" and the test goes under "test/java"

Not quite right. If you look at the example that Tim gave, the packages are actually *the same* except that the unit tests are organized under a different directory in the file system (src/test/ instead of src/main). Also, integration tests in a Maven project are usually placed in the same parallel package structure under the src/it folder.
 
Geane Norm
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
Tapas Chand wrote:If you look at the above structure given by Tim, it is not only different package for testing, but the parent directories are different altogether.
When you create a maven project, the source goes under "src/main/java" and the test goes under "test/java"

Not quite right. If you look at the example that Tim gave, the packages are actually *the same* except that the unit tests are organized under a different directory in the file system (src/test/ instead of src/main). Also, integration tests in a Maven project are usually placed in the same parallel package structure under the src/it folder.


Thank you Junilu and thank you all for getting back to me. I am not very familiar with Maven and instead I wanted to create the Maven-like structure directly within Eclipse (as a first attempt). What puzzles me (and I am sure it is still inexperience) is that if I re-create the standard directory layout in Eclipse, do I treat main and java, for example, as folders whereas my package will be com.myName.demo? Will the package resides inside the folder java? I definitively went wrong with creating my structure from scratch and ended up with the error 'java.lang.SecurityException: Prohibited package name' because my package name had java in it.

Thanks for your help,

I
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're manually configuring your build path, you need to remove all source folders first, then add the folders right above com.whatever as source folders. So in the structure that Tim gave, your source folders would be src/main/java and src/test/java. Your unit tests go in package structure in src/test/java that mirrors the package structure in src/main/java. This essentially puts a unit test class in the same package as the class it is testing, which allows you to access package private scoped members (methods, fields, etc.) of the class under test.
 
Tim Holloway
Saloon Keeper
Posts: 18789
74
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke's sample directory structure is the standard for projects built with Maven. So not only does it organize mainline code and test code/data into separate compartments, it allows Maven to be used to automatically compile code and run tests with minimal project-specific configuration.

Keeping test and mainline code/data/classes in separate physical directories has many benefits. You shouldn't be incorporating test code or data into production builds, so by only using the mainline source to build production components, you don't get all the extra bulk of test code (and possible security risks as well!)

Eclipse fits well into this scheme. Instead of dumping everything Eclipse compiles into one big folder, you can assign specific class folders to specific source folders so that the test classes remain segregated from the mainline classes but still appear in the classpath when running tests from within Eclipse.

 
Tim Bee
Ranch Hand
Posts: 91
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Holloway wrote:Tim Cooke's sample directory structure is the standard for projects built with Maven. So not only does it organize mainline code and test code/data into separate compartments, it allows Maven to be used to automatically compile code and run tests with minimal project-specific configuration.

Keeping test and mainline code/data/classes in separate physical directories has many benefits. You shouldn't be incorporating test code or data into production builds, so by only using the mainline source to build production components, you don't get all the extra bulk of test code (and possible security risks as well!)

Eclipse fits well into this scheme. Instead of dumping everything Eclipse compiles into one big folder, you can assign specific class folders to specific source folders so that the test classes remain segregated from the mainline classes but still appear in the classpath when running tests from within Eclipse.



I went to your blog and tried to run the same command. I got a Build Failure. Any help?



C:\Program Files\Apache Software Foundation\apache-maven-3.3.1>mvn archetype:gen
erate -DinteractiveMode=false -DarchetypeGroupId=org.apache.maven.archetypes -Dg
roupId=com.trcooke.mvndemo -DartifactId=maven-demo
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.3:generate (default-cli) > generate-sources
@ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:2.3:generate (default-cli) < generate-sources
@ standalone-pom <<<
[INFO]
[INFO] --- maven-archetype-plugin:2.3:generate (default-cli) @ standalone-pom --
-
[INFO] Generating project in Batch mode
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.
archetypes:maven-archetype-quickstart:1.0)
[INFO] -------------------------------------------------------------------------
---
[INFO] Using following parameters for creating project from Old (1.x) Archetype:
maven-archetype-quickstart:1.0
[INFO] -------------------------------------------------------------------------
---
[INFO] Parameter: basedir, Value: C:\Program Files\Apache Software Foundation\ap
ache-maven-3.3.1
[INFO] Parameter: package, Value: com.trcooke.mvndemo
[INFO] Parameter: groupId, Value: com.trcooke.mvndemo
[INFO] Parameter: artifactId, Value: maven-demo
[INFO] Parameter: packageName, Value: com.trcooke.mvndemo
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 10.524 s
[INFO] Finished at: 2016-02-22T13:16:21-05:00
[INFO] Final Memory: 14M/155M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:2
.3:generate (default-cli) on project standalone-pom: Error merging velocity temp
lates -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e swit
ch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please rea
d the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureExc
eption

C:\Program Files\Apache Software Foundation\apache-maven-3.3.1>
 
Tim Holloway
Saloon Keeper
Posts: 18789
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just tried it on Linux using JDK 1.7 and Maven version 2.2.1 - which admittedly is fairly old. It worked fine.

One thing that MIGHT have caused problems is if you had wrapping on the command line. That command is all one long line.
 
Tim Holloway
Saloon Keeper
Posts: 18789
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Update: it still works as of Maven 3.3.3. In Linux, the environment SET commands are like this:



Results:

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!