Setup with ANT
In this how-to tutorial I will use Apache Ant to manage a Citrus project and we will use Ant to execute Citrus test cases. First of all I loaded the latest Citrus release archive to an empty folder on my local storage. Unzip the archive in order to have access to the Citrus binaries and sources.
You need following software on your computer, in order to use the Citrus Framework:
- Java 11 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).
- Ant 1.8 or higher Citrus tests will be executed with the Apache Ant build tool. But it is not required to use Ant only. You can also run tests via Apache Maven for example.
So now lets start to set up a new Citrus Java project. In contrast to a Maven generated project we have to create our project structure ourself when using Ant. A good starting point is the samples folder coming with the Citrus release distribution. Here you can see several sample Citrus projects with given folder structure. So lets have a look at a usual Citrus project structure. I am using the Eclipse IDE development tool to set up the new Java project. I am also accessing Ant from the command line in this tutorial so you may also want to install Ant first before continuing. See the Ant homepage for detailed installation instructions.
Have a look at the folder structure I created for our new Citrus project
I created a simple Java project in Eclipse called “citrus-sample”. I manually added folders and files to the project as follows. The Citrus project knows three source folders, that are also added to the Java build path as source folders:
- src/it/java: Storage for generated Java TestNG tests
- src/it/resources: Configuration files go in here (e.g. Spring application context files, citrus.properties, log4j.xml, …)
- src/it/tests: XML test case describing files (generated by Citrus)
As a next step we setup a lib folder for some libraries we need for execution. This includes the Citrus Java archives (citrus-core.jar, citrus-http.jar, etc.) coming from the downladed release archive and all dependency libraries also available in the release archive. Finally what’s missing is the Ant build file (build.xml) with following content.
Those who are familiar with Ant will understand this file easily. We used the maven ant tasks in order to download all 3rd party libraries needed for a Citrus run. You need to setup the Maven Ant tasks JAR and the TestNG JAR in the lib directory before executing the build script for the first time. Further a Java classpath is defined with its conventional name “citrus-classpath”. The classpath definition includes all Java libraries found inside the lib folder. Besides that the build file declares the TestNG special Ant tasks that help us to execute the tests.
These TestNG Ant task definitions will help to execute Citrus out of a Ant build script. We will handle the execution later in this tutorial in more detail. For now we will start with the “create.test” target. This target creates new test cases for your project. So let us give it a try! I execute the “create.test” target from the command line using my seperate Ant installation. To be honest it is more comfortable to use the built-in Eclipse Ant plugin to execute Ant targets, but I choose the command line first to be more independent from the IDE tool.
The target will prompt for some information when executed, see the output below:
The target will ask you for the name of the test first. After that you give a description for the test. The author’s default name is located in the “citrus.properties”, but you can also specify another author of course. Same thing with the test’s package. In the “citrus.properties” you can define a default package or you can type in another package when prompted.
Now that we have given all information for the test Citrus will create all test files automatically. Let us have a look at the generated files.
The generated files are:
- src/it/java/MyFirstTest.java: TestNG Java test case that is executable right now
- src/it/tests/MyFirstTest.xml: Citrus XML test case describing file
With these two files you are able to run the test. Let us first have a look at the generated XML test description:
Citrus created an empty test case that does nothing but print a simple message to the console. The test is in status DRAFT which means it is not finished yet but be aware that the test is executable in this state. In case you need to disable the test because it is not entirely finished yet and may cause failures in a test run you can use the status DISABLED. You may code the test first and then change its status to FINAL in order to finally enable the test for execution. Right now we do not want to code any additional logic into the test case for this tutorial, so we change its status right now to FINAL and try to execute the test with Ant.
We switch again to the command line to execute the Ant target called “citrus.run.single.test”. We are asked to type in the name of the test to execute. In our example we type “MyFirstTest” and Citrus will execute the test.
The execution by test names only is suitable for executing a small amount of test cases. If you want to run a whole package of tests use the package notation instead as it is used in the “citrus.run.tests” target which executes all tests in the package “com.consol.citrus”.
The last option you have when executing Citrus is a TestNG suite xml file. TestNG can configure test suites via XML. We have a look at a very simple testng.xml.
The testng.xml is very powerful and you have a lot of options how to group tests together to suites. Citrus can work on those testng.xml files when executed with Ant.
That’s it! This is how Citrus connects with a normal Eclipse Java project and Ant. With Ant you can integrate your Citrus tests easily in your continuous build environment, so the tests are executed every time something has changed in the code base of your project.