While creating a new Maven project, the directories src/main/java and src/test/java are created. With some googling, I came to know that my main source code that I use for testing must be placed in src/main/java. But then, what is the purpose of two separate directories. The current answer on a similar question didn't help much.
5 Answers
Maven and other build management environments (e.g. gradle) are based on the assumption that you do automated testing via e.g. unit tests. For that you need extra code for testing that should not be included in your final product delivered to your customer.
Thus, everything that goes into src/main/java is per default packaged into the product that you would deliver for your customer whereas everything that you put into src/test/java is not.
This is an advantage for various reasons:
- your delivered products are smaller
- it is easier to to find testrelated code inside your project
- you can load various libraries only for testing.
- ...
1 Comment
As per the Maven configurations, tests class will be found in the src/test directory and the source code will be found in the src/main directory. So src/main/java is the root directory for your source code & src/test/java/ is the root directory for your test code.
Ex: Hotel Package, Reservation class
Source Class file : src/main/java/Hotel/Reservation.java
Test Class file : src/test/java/Hotel/ReservationTest.java
Comments
src/main/java places your code that use for real production.
src/test/java places your test use case code, like junit test. These codes would be executed when doing maven package things. These codes won't be packaged to your war or jar file. Which means these codes won't for real production.
Plus: Unit test codes are not required to be packaged in production. You don't need to and should not to put them in src/main/java folder.
Comments
To make types easier to find and use, to avoid naming conflicts, and to control access, programmers bundle groups of related types into packages.here
src/testis not included in the production build.mainis for production,testis only for testing before putting into production.