Maven project has following folder structure:
src/main/java
src/main/resources
src/test/java
src/test/resources
If we navigate to maven project folder and then hit mvn clean test. This command will clean the project then build the project and run the tests
But, I didn't understand which tests it will run.
In which folder, of above folder structure, it will look for tests to execute? (Maybe src/test/java?)
Based on what, it will determine if particular method is test? (Maybe TestNG or JUnit test methods?)
Maven uses Surefire test plugin by default.
It looks for the unit tests in src/test/java as you correctly guessed in your question.
Surefire supports several unit testing frameworks, such as JUnit and TestNG or POJO tests. So if you have both TestNG and JUnit tests in src/test/java and have appropriate test dependencies for both frameworks in your pom.xml both JUnit and TestNG tests will be executed.
Related
I added the maven-failsafe-plugin to my project for runnig integration tests. When i run test only unit tests are running and when i run integration-test first unit tests and then integration tests are running.
It's all what i want so far but when i clean and build the project both unit tests and integration tests are running which i wouldn't prefer because i just don't want to prepare the environment for integration testing on every build.
I clean and build the project for some generated source to regenerate and i don't want to run the integration test meanwhile.
Is there a way that i only run the integration tests on purpose not by any automation?
I added -DskipITs flag clean install command of maven so integration tests are not running when i make a call of clean and install.
And i created maven goals as integration-test and verify to run and verify the integration tests via the failsafe plugin which i added to my pom.xml.
As i'm using netbeans as ide, i set the predefined maven commands via the actions tab of the project properties and i can run the integraion tests via right click to peoject and integration test sub menu under maven menu.
I have a spring-boot application which includes JUnit tests and I build using maven. I have organized by JUnit tests in a Suite using #Suite.SuiteClasses notation.
My problem/question is why when I run mvn package locally, I see that the unit tests referenced by the Suite are executed but they are also executed as if they were not part of the suite, while building the code in Bamboo, using again mvn package executes the tests only once (i.e. as members of the suite).
Do you use the same profiles / settings / maven commands on bamboo and locally?
I think what happens is that when executing the package phase the surefire-plugin starts executing all the tests. There might be a naming issue with the includes the surefire plugin uses by default.
If you execute the maven goal with -X you should be able to see the surefire-plugin configuration it uses to identify the tests. This should only match your suites - not the suites and the tests themselves.
I have some junit tests and htmlunit integration tests in one of my maven project. The problem is that my integration tests are not getting executed when I run
mvn clean test
Junit Tests Here:
webstore\src\test\java\com\istore\dao\AddressTest.java
Integration Tests Here:
webstore\src\test\java\com\istore\presentation\htmlunit\PageTests.java
How does mvn determines that AddressTest.java should execute and other one should not?
http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#includes describes the expected filepatterns.
btw, if you are talking about integration tests, have a look at the maven-failsafe-plugin. It uses *IT.java as file pattern
The problem is suffix Tests, should be Test in your PageTests.java!
By default, the Surefire Plugin will automatically include all test classes with the following wildcard patterns:
**/Test*.java - includes all of its subdirectories and all java filenames that start with Test.
**/*Test.java - includes all of its subdirectories and all java filenames that end with Test.
**/*TestCase.java - includes all of its subdirectories and all java filenames that end with TestCase.
Maven Failsafe Plugin is for integration tests and uses suffix IT. To invoke integration tests with Failsafe Plugin use
mvn verify
References:
Inclusions and Exclusions of Tests in Surefire
Inclusions and Exclusions of Tests in Failsafe
Problem resolved after executing following command:
mvn failsafe:integration-test
i have created a multi-module maven project and i am trying to write and execute some tests on specific modules. I am trying to put all test-code into a separate module, but i am wondering if this is the correct way and if so how to i setup the maven build/test cycle so mvn install will use these tests?
According to the Maven Standard Directory Layout test classes belong to the directory src/test/java and required resources to src/test/resources. On the long term it will make your life easier by following the rules, especially when you work on many different projects (where you sometimes can not define alternative structures). In this setup the test cases are automatically invoked by mvn install or mvn test.
You find more informations about the surefire-plugin (responsible for executing the tests) here.
could you please share your way of unit testing in eclipse ? Are you using surefire plugin, m2eclipse & maven, or only testNG eclipse plugin ? Do you combine these alternatives ?
I'm using testNG + maven surefire-plugin and I had been using the testNG eclipse plugin a year ago so that I could see the results in testNG view. Then I started using Maven, but when I do "maven test phase" using m2eclipse, there is only console output and surefire reports that I can check in browser and to choose what test suite, test, or test method can be set up only via testng.xml.
On the other hand, if you use only surefire plugin and you have some specific settings regarding classpath etc., that you rely on, then running tests via testNG eclipse plugin doesn't have to be compatible with your code. Using surefire plugin, the classpath is different - target/test-classes and target/classes - than using testNG plugin, that is using the project classpath.
How do you go about what I was just talking about?
Is it possible to synchronize "maven test" using m2eclipse and surefire plugin WITH testNG eclipse plugin and view ?
EDITED: I'm also wondering, why the Maven project ("Java build path") output folder is target/classes for src/main and src/test whereas surefire plugin makes two locations target/test-classes and target/classes
Thank you very much for your your opinions.
Lisa,
You can configure the TestNG Eclipse plug-in to "watch" a test-output directory. Point it to target/surefire-reports and you should see your TestNG view update itself a few seconds after a Maven build terminates.
I see two advantages of using the surefire plugin:
Relying on the eclipse plugin only
works that way when everyone in the
project uses eclipse
Surefire plugin
can run from build that are done from
the continuous integration server
("jenkins")
And then if you have larger more long running (performance) test suites, you probably don't want to "block" your IDE while they run.
I don't think it is true what you say with the different runtime classpath, I just checked myself both maven classpath and the one when test is run via testNG eclipse plugin and both are the same. I think that m2eclipse plugin takes care of it. It's the same even for junit testing.
So afaik, there is no restriction for using testNG eclipse plugin for development and surefire plugin for instance for continuous integration as Heiko Rupp mentions. At least I have never gotten any troubles with it.
Just use both as you like, cheers !