Maven: pom.xml, scope, Surefire and Failsafe, running tests
Maven builds a project from the description in pom.xml: coordinates (groupId, artifactId, version), dependencies, plugins, properties. The standard layout: src/main/java, src/test/java, src/test/resources. In a tests-only project the code lives in src/test.
- A dependency with
<scope>test</scope>is available only when compiling and running tests and does not enter the artifact. Test libraries (JUnit, AssertJ, REST Assured) have this scope. - Versions go into
<properties>or a BOM (dependencyManagement) so they do not drift.mvn dependency:treeshows where a conflicting version came from. - The lifecycle:
validate → compile → test → package → verify; a command runs all phases up to the given one. That is whymvn verifyalso runs Failsafe tests.
Surefire runs unit tests in the test phase (by default classes *Test, Test*, *Tests, *TestCase). Failsafe is meant for integration tests (*IT, IT*, *ITCase); its goals are integration-test and verify. A failure does not stop the build at once, so the post-integration-test phase (shutting down the test environment) still runs, and verify checks the result. Run mvn verify, not mvn integration-test. Automation projects for UI/API often use only Surefire, and Failsafe when tests run after services are brought up.
Useful runs:
mvn test -Dtest=LoginTest # one class
mvn test -Dtest=LoginTest#validLogin # one method
mvn test -Dgroups=smoke # JUnit 5/6 tags / TestNG groups
mvn test -DexcludedGroups=slow
mvn verify -Pstaging -Dbrowser=firefox # a profile and a system property
mvn -DskipTests package # compiles tests but does not run them
mvn -Dmaven.test.skip=true package # does not even compile tests
A profile (<profile>) changes dependencies, properties or plugin configuration for an environment. User properties from -D are passed by Surefire to the test process, so in code they are available through System.getProperty("browser", "chrome").