Dheemanth Bhandarkar
Dheemanth Bhandarkar

Reputation: 360

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

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.

Upvotes: 12

Views: 32624

Answers (5)

Chamara Maduranga
Chamara Maduranga

Reputation: 256

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

Upvotes: 3

Vinod Bokare
Vinod Bokare

Reputation: 86

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

Upvotes: 0

lwi
lwi

Reputation: 1712

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.
  • ...

Upvotes: 24

Bejond
Bejond

Reputation: 1198

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.

Upvotes: 2

Moritz Petersen
Moritz Petersen

Reputation: 13057

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.

Upvotes: 2

Related Questions