13

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.

3
  • 4
    The code under src/test is not included in the production build. Commented Apr 24, 2018 at 7:04
  • What do you mean by "main source code that I use for testing"? Commented Apr 24, 2018 at 7:09
  • 2
    main is for production, test is only for testing before putting into production. Commented Apr 24, 2018 at 7:09

5 Answers 5

25

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.
  • ...
Sign up to request clarification or add additional context in comments.

1 Comment

Also note that tests are run as part of the normal Maven build, therefore they need to be fast ("unit tests") and not depend on infrastructure. My personal conclusion is that integration tests etc should go in another project. I believe this to be a design shortcoming in Maven.
3

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

2

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.

Comments

2

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

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.