What is the difference between 'mvn verify' vs 'mvn test'?

Vivek picture Vivek · Mar 30, 2021 · Viewed 13.1k times · Source

I'm bit confused with mvn verify phase. I've created a Spring Boot project (a simple project, without any explicit configurations added). I've created a few JUnit unit tests which are run with both the mvn verify and mvn test commands.

There isn't any difference observed in the mvn verify and mvn test command output.

What does mvn verify do different than mvn test?

Also some posts on Stack Overflow mentions that mvn verify runs the integration tests. If this is the case then I have few questions.

  • How does Maven identify a specific test as a unit test or integration test?
  • If mvn verify is supposed to run only the integration tests, then why are unit tests executed with it?

Answer

UrbanoJVR picture UrbanoJVR · Mar 30, 2021

First of all, when you run a Maven goal, it will run any previous goal. The order of basic phases is:

  • Validate
  • Compile
  • Test
  • Package
  • Verify
  • Install
  • Deploy

If you run Test, Maven will execute validate, compile and test. Based on this, the first point is that verify includes test.

Based on official documentation:

  • TEST - test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed

  • VERIFY - run any checks on results of integration tests to ensure quality criteria are met

To run unit tests, the Surefire plugin is recommended. And Failsafe for integration tests.

The verify command executes each default lifecycle phase in order (validate, compile, package, etc.), before executing verify. In most cases the effect is the same as package. However, in case there are integration tests, these will be executed as well. And during the verify phase some additional checks can be done, e.g. if your code is written according to the predefined checkstyle rules.

Conclusion: if you want to run your integration tests and check it, use verify. If you only want to run unit tests, use test.

My personal advice: if in doubt, use verify.