Setup with Maven
This quickstart shows you how to setup a new Citrus project with Maven. You will see it is very easy and you are finished within minutes.
You need following software on your computer, in order to use the Citrus Framework:
- Java 8 or higher Installed JDK plus JAVA_HOME environment variable set up and pointing to your Java installation directory
- Java IDE A Java IDE will help you manage your Citrus project, create and execute test cases. Just use the Java IDE that you are used to (e.g. Eclipse or IntelliJ IDEA).
- Maven 3.0.x or higher Citrus tests will be executed with the Apache Maven build tool.
In case you already use Maven build tool in your project it is most suitable for you to include Citrus into your Maven build lifecycle. In this tutorial we will setup a project with Maven and configure the Maven POM to execute all Citrus tests during the Maven integration-test phase.
First of all we create a new Java project called citrus-sample. We use the Maven command line tool in combination with Maven’s archetype plugin. In case you do not have Maven installed yet it is time for you to do so before continuing this tutorial. See the Maven site for detailed installation instructions. So let’s start with creating the Citrus Java project:
In the sample above we used the Citrus archetype available in Maven central repository. As the list of default archetypes available in Maven central is very long, it has been filtered for official Citrus archetypes.
After choosing the Citrus quickstart archetype you have to define several values for your project: the groupId, the artifactId, the package and the project version. After that we are done! Maven created a Citrus project structure for us which is ready for testing. You should see the following basic project folder structure.
citrus-sample | + src | | + main | | | + java | | | + resources | | + test | | | + java | | | + resources pom.xml
The Citrus project is absolutely ready for testing. With Maven we can build, package, install and test our project right away without any adjustments. The project comes with a sample Citrus test SampleIT. You can find this test in src/test/resources; and src/test/java. The Citrus test was automatically executed in the integration test phase in Maven project lifecycle.
The next step would be to import our project into our favorite IDE (e.g. Eclipse, IntelliJ or NetBeans). Please follow the instructions of your IDE to integrate with Maven projects.
Now open the new Citrus project with the Java IDE and have a closer look at the project files that were generated for you. First of all open the Maven POM (pom.xml). You see the basic Maven project settings, all Citrus project dependencies as well as Maven plugin configurations. In future you may add new project dependencies and Maven plugins in this file. For now you do not have to change the Citrus Maven settings in your project POM, however we have a closer look at them:
The Citrus project libraries are loaded as dependencies in our Maven POM.
The Citrus Maven plugin capable of test creation and report generation.
The surefire and failsafe plugin configuration is responsible for executing all available tests in your project. Unit tests are traditionally executed with maven-surefire. Integration tests are executed with maven-failsafe. As Citrus tests are integration tests these tests will be executed in the Maven integration-test phase using maven-failsafe:
The tests are separated by naming convention. All integration tests follow the **/IT.java** or ****/IT.java naming pattern. These tests are explicitly excluded from surefire plugin. This makes sure that the tests are not executed twice. Now you are ready to use both unit and integration tests in your Maven project.
Another notable POM configuration is the repository section. The Citrus stable release versions are available on Maven central repository. So Maven will automatically download the artifacts from that servers. Now if you want to use the latest snapshot preview version of Citrus you need to add the ConSol Labs repository in your POM.
Please note that this setting is optional and only applies for snapshot versions of Citrus.
The sample application uses Maven as build tool. So you can use the Maven commands to compile, package and test the sample.
Congratulations! You just have built the complete project and you also have executed the first Citrus tests in your project.
If you just want to execute the Citrus tests with a Maven build execute
This executes all Citrus test cases during the build and you will see Citrus performing some integration test logging output. You can also execute single test cases by defining the test name as Maven system property.
Finally we are ready to proceed with creating new test cases. So let’s add a new Citrus test case to our project. We use the Citrus Maven plugin here, just type the following command:
You have to specify the test name, author, description, package and the test framework. The plugin successfully generates the new test files for you. On the one hand a new Java class in src/it/java and a new XML test file in src/it/tests. The test is runnable right now. Try it and execute "mvn clean verify" once more. In the Citrus test results you will see that the new test was executed during integration-test phase along with the other existing test case. You can also run the test manually in your IDE with a TestNG plugin.
So now you are ready to use Citrus! Write test cases and add more logic to the test project. Have fun with it!