57

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?
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vivek
  • 11,938
  • 19
  • 92
  • 127
  • 2
    @JFabianMeier I agree, but the documentation is very confusing to me in terms of distinguishing what different `verify` does apart from executing additional phases than `test`. Also `checks on results of integration tests to ensure quality criteria` is not very clear, what checks? why only integration tests? where is quality criteria configured? – Vivek Mar 31 '21 at 09:49
  • Here you can see what happens in the different phases for a JAR: https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#packaging Of course, one can bind additional goals to phases. – J Fabian Meier Mar 31 '21 at 10:39

2 Answers2

75

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
UrbanoJVR
  • 1,010
  • 1
  • 7
  • 11
  • 3
    **to ensure quality criteria** - where is this criteria configured? – Vivek Mar 30 '21 at 19:16
  • It depends on your configuration and plugins that are you using. Usually, you configure rules for integration tests with failsafe plugin. Integration tests only will be executed with verify (with mvn test only unit), and then, you can configure for example to break the execution if one test fail, or you can configure to complete all build without break even if there are tests failures. There are a lot of maven plugins and you can attach a concrete action to any phase. The "rules" is marked on all of your pom configuration. From execute a command or even up a docker container.... – UrbanoJVR Mar 30 '21 at 22:30
  • 1
    That kind of configuration is so important in your CI environment. Continuing with the same example, if your build break when a IT test fail, your Merge Request will mark that execution failed. If not, the people will see it "in green". – UrbanoJVR Mar 30 '21 at 22:32
1

How does Maven identify a specific test as a unit test or integration test?

Integrations Test always takes a name like IT.java or IT.java or *ITCase.java

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Sumit Sharma May 23 '22 at 06:59