paint-brush
Experimenting With Citrus Integration Testing Frameworkby@taacoder
1,454 reads
1,454 reads

Experimenting With Citrus Integration Testing Framework

by Alexey TaranenkoApril 20th, 2021
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Each Citrus test is a Java class representing a JUnit or TestNG unit test of your choice. If you have not worked with this framework before, you will feel comfortable, since Citrus uses these libraries internally. We will create a test class named “FirstTest” and use it to test our integration tests. We can extend our class using the TestNG or Junit libraries built into Citrus. We need the “Citrus quickstart project” version I recommend choosing 2.8.0.0.
featured image - Experimenting With Citrus Integration Testing Framework
Alexey Taranenko HackerNoon profile picture

In this article, I want to tell you how you can create a test project on the Citrus Framework in order to further develop it, that is, write integration tests. I want to note that if you have not worked with this framework before, but let's say you worked with Junit or TestNG, then you will feel comfortable, since Citrus uses these libraries internally. So each Citrus test is a Java class representing a JUnit or TestNG unit test of your choice.

Okay, for an easy start, let's generate a project from the command line, send the following for execution:

mvn archetype:generate -Dfilter=com.consol.citrus.archetypes:citrus

Next, you will be prompted to generate a Citrus project of your choice:

Choose archetype:
1: remote -> com.consol.citrus.archetypes:citrus-quickstart (Citrus quickstart project)
2: remote -> com.consol.citrus.archetypes:citrus-quickstart-jms (Citrus quickstart project with JMS consumer and producer)
3: remote -> com.consol.citrus.archetypes:citrus-quickstart-soap (Citrus quickstart project with SOAP client and server)
4: remote -> com.consol.citrus.archetypes:citrus-simulator-archetype-jms (Archetype for Citrus JMS simulator)
5: remote -> com.consol.citrus.archetypes:citrus-simulator-archetype-mail (Archetype for Citrus mail simulator)
6: remote -> com.consol.citrus.archetypes:citrus-simulator-archetype-rest (Archetype for Citrus REST simulator)
7: remote -> com.consol.citrus.archetypes:citrus-simulator-archetype-swagger (Archetype for Citrus Swagger auto generated REST simulator)
8: remote -> com.consol.citrus.archetypes:citrus-simulator-archetype-ws (Archetype for Citrus SOAP simulator)
9: remote -> com.consol.citrus.archetypes:citrus-simulator-archetype-wsdl (Archetype for Citrus WSDL auto generated SOAP simulator)

We need the “Citrus quickstart project”, so choose the number: 1.

“Citrus quickstart project” version I recommend choosing 2.8.0.

Further, for example:

Define value for groupId: com.citrus.projects
Define value for artifactId: citrus-sample-project
Define value for version: 1.0-SNAPSHOT
Define value for package: com.citrus.projects

After the installation is complete, find the folder where the project was generated and open it in the development environment. Go to “projectName/src/ java/citrus” and create a class named for example “FirstTest”. This will be our first Citrus test.

As I wrote at the beginning, we can extend our class using the TestNG or Junit libraries built into Citrus, so let's do this:

For TestNG:

public class FirstTest extends TestNGCitrusTestRunner {

    @Test
    @CitrusTest
    public void getTestActions() {

    }
}

For JUnit:

public class FirstTest extends JUnit4CitrusTestRunner {

    @Test
    @CitrusTest
    public void getTestActions() {

    }
}

And as you noticed, I marked the test method with annotations @Test (which refers to TestNG or JUnit) and @CitrusTest (which Citrus Framework itself requires).

Before we add actions to our getTestActions() method, let's configure, for example, a REST client that will send requests to and receive responses from the service under test. We will configure the client in the “citrus-context.xml” file in the “projectName/src/resources” folder.

Let's add the following:

<citrus-http:client id="restClient"
                    request-url="https://service_host:8080/api/"
                    content-type="application/json"
                    timeout="60000"/>

Now let's get back to our “FirstTest”. Let's create a client instance using the @Autowired annotation. Next, let's add actions as described below.

public class FirstTest extends TestNGCitrusTestRunner {

    @Autowired
    private HttpClient restClient;

    @Test
    @CitrusTest
    public void getTestActions() {

        http(httpActionBuilder -> httpActionBuilder
                .client(restClient)
                .send()
                .get("users/2")
        );

        http(httpActionBuilder -> httpActionBuilder
                .client(restClient)
                .receive()
                .response()
                .messageType(MessageType.JSON)
                .payload("{\n" +
                        "   \"data\":{\n" +
                        "      \"id\":2,\n" +
                        "      \"first_name\":\"Janet\",\n" +
                        "      \"last_name\":\"Weaver\",\n" +
			"   }\n" +
                        "}")
        );
    }
}

Two actions are shown here, where the first one sends a GET request to url: “https://service_host:8080/api/users/2”, the second receives a synchronous response and checks the received result with what we specified in “payload”. As a result of running the test, we get the result of passing the test "passed" or "failed".

So, we have created a simple test for testing a REST service. You can now extend this test project further by adding other tests. For those who are interested in Citrus Framework, I leave a link to the site “www.citrusframework.org”.

That's all for now.

I wish you all successful testing!