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.
The main folder contains your application code and resources, and the test folder contains, well, test code and resources. So don't copy your application code there, but only the tests. The test sources are then automatically added to the classpath in the test phases.
1. The SRC test examines the water absorption and retention profile of gluten proteins, damaged starch and pentosans by using four different types of solvents: water, sucrose, sodium carbonate and lactic acid.
The src/main Directory As the name indicates, src/main is the most important directory of a Maven project. Anything that is supposed to be part of an artifact, be it a jar or war, should be present here. Its subdirectories are: src/main/java – Java source code for the artifact.
The src directory contains all of the source material for building the project, its site and so on. It contains a subdirectory for each type: main for the main build artifact, test for the unit test code and resources, site and so on.
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:
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.
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
The reason to have test code and production code (src/main/java
) separate is, that it is easier to build the application by just including production code.
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