This sample the usage of Citrus test behaviors when sending and receiving messages to the todo sample
application. Read about test behaviors in reference guide
The todo-list sample application provides a REST API for managing todo entries.
We call this API and receive Json message structures for validation in our test cases.
This time we want to reuse specific message exchange logic by using test behaviors in our tests. The test behavior is a
class that provides a set of test actions for reuse in other test cases. A typical test behavior looks like this:
The behavior extends AbstractTestBehavior and defines its logic in the apply method where you can use all Citrus Java fluent API methods as you would do in a normal test.
The behavior is then applied in your test as follows:
The sample above applies the behavior in the test multiple times with different names. The result will be multiple echo test actions that print out the messages to the console logging.
Now we can use behaviors in order to add new todo entries via Http POST request.
As you can see the behavior provides support for Json payload inline data as well as file resource payloads. You can use the behavior in test then as follows:
The behavior is reused multiple times with different payload input. Now we can add more behaviors for getting todo entries via Http GET requests.
This time the behavior provides different approaches how to validate the todo entry that was sent as a Json response payload. We can use payload inline data, file resource and JsonPath expressions:
This completes the usage of test behaviors in Citrus. This is a great way to centralize common tasks in your project to reusable pieces of Citrus Java DSL code.
You can execute some sample Citrus test cases in this sample in order to write the reports.
Open a separate command line terminal and navigate to the sample folder.
Execute all Citrus tests by calling
You should see Citrus performing several tests with lots of debugging output.
And of course green tests at the very end of the build and some new reporting files in target/citrus-reports folder.
Of course you can also start the Citrus tests from your favorite IDE.
Just start the Citrus test using the TestNG IDE integration in IntelliJ, Eclipse or Netbeans.