Video in progress
Where do you write tests in a Maven project, and how does mvn test find them?
Tests live in src/test/java and test resources in src/test/resources. mvn test uses the Surefire plugin to find classes whose names end in *Test, Test*, *Tests or *TestCase and run them.
📖 Answer
The structure mirrors the main code:
src/main/java/uz/shop/order/OrderService.java
src/test/java/uz/shop/order/OrderServiceTest.java
src/test/resources/application-test.yml
Because the test class is in the same package as the main class, it can also reach package-private methods.
Surefire's default name pattern matters. If you name a class OrderServiceSpec, it runs when you click it in the IDE, but mvn test doesn't see it: CI is green and the test never ran at all. This happens in real projects.
Integration tests are usually separated: their names end in *IT, and the Failsafe plugin runs them in the mvn verify phase. That way the fast unit tests go first and the slow ones after.
mvn test -Dtest=OrderServiceTest # one class
mvn test -Dtest=OrderServiceTest#cancel* # by method name
Gradle has the same folder structure, and you run it with ./gradlew test.
Sign up to mark studied questions and save your progress.