This sample demonstrates how to use TestNG data providers in Citrus tests. You can also read about this in reference guide.

Objectives

The todo-list sample application provides a REST API for managing todo entries. Citrus is able to call the API methods as a client in order to add new todo entries. In this sample we make use of the TestNG data provider feature in terms of adding multiple todo entries within on single test.

The data provider is defined in the test case.

@DataProvider(name = "todoDataProvider")
public Object[][] todoDataProvider() {
    return new Object[][] {
        new Object[] { "todo1", "Description: todo1" },
        new Object[] { "todo2", "Description: todo2" },
        new Object[] { "todo3", "Description: todo3" }
    };
}

The provider gives us two parameters todoName and todoDescription. The parameters can be bound to test variables in the Citrus test with some annotation magic.

@Test(dataProvider = "todoDataProvider")
@CitrusTest
@CitrusParameters( { "todoName", "todoDescription" })
public void testProvider(String todoName, String todoDescription) {
    variable("todoId", "citrus:randomUUID()");

    http()
        .client(todoClient)
        .send()
        .post("/todolist")
        .messageType(MessageType.JSON)
        .contentType("application/json")
        .payload("{ \"id\": \"${todoId}\", \"title\": \"${todoName}\", \"description\": \"${todoDescription}\"}");
    
    [...]    
}            

As you can see we are able to use the name and description values provided by the data provider. When executed the test performs multiple times with respective values:

CITRUS TEST RESULTS
TodoListIT.testPost([todo1, Description: todo1]) ............... SUCCESS
TodoListIT.testPost([todo2, Description: todo2]) ............... SUCCESS
TodoListIT.testPost([todo3, Description: todo3]) ............... SUCCESS    

Run

You can run the sample on your localhost in order to see Citrus in action. Read the instructions how to run the sample.