Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between src/main/java and src/test/java in a Maven project

Tags:

java

maven

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.

like image 949
Dheemanth Bhandarkar Avatar asked Apr 24 '18 07:04

Dheemanth Bhandarkar


People also ask

What is main and test in Maven?

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.

What is src test?

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.

What is src Main?

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.

What is src in Maven?

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.


4 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.
  • ...
like image 168
lwi Avatar answered Nov 15 '22 19:11

lwi


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.

like image 21
Bejond Avatar answered Nov 15 '22 18:11

Bejond


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
like image 29
Chamara Maduranga Avatar answered Nov 15 '22 18:11

Chamara Maduranga


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.

like image 20
Moritz Petersen Avatar answered Nov 15 '22 20:11

Moritz Petersen