How do we make mvn build or Gradle build to take test cases from the main package if we have written test cases in the main package?
2 Answers
In gradle you just redefine the main and test sourceSets with filters in your build.gradle
file to exclude / include the test files in the specific phase of the build.
For example the file named LibraryTest.java
will be compiled and executed only during the test phase.
sourceSets {
main {
java {
srcDirs = ['src/main/java']
exclude '**/*Test.java'
}
}
test {
java {
srcDirs = ["src/main/java"]
include '**/*Test.java'
}
}
}

- 2,294
- 1
- 15
- 26
-
@pankaj - don't forget to accept the answer if it worked for you. Thanks! – Oresztesz Apr 17 '20 at 07:25
you can write your test cases where ever you want, BUT!
It is not a recommended practice, so if you are using maven/gradle - they will give you dedicated folder/path for writing test cases.
The reason why it is not recommended - is maven/gradle provides lot of plugins which will help you to run your test cases, generate reports for those test cases, control the build if test cases fails.
All these plugins will look up at the default path, so if you decide to use a different path rather than default - you need to change the path for test cases in all your plugin.
so if you choose to use your own path for test resources, you are just adding overhead of additional configuration changes.

- 1,414
- 2
- 18
- 35
-
"All these plugins will look up at the default path": For me this sounds like a bug in the plugins then. All plugins should respect source set settings. Nonetheless, a clear separation of source sets is still recommended. – thokuest Apr 17 '20 at 11:22