Applies to ReadyAPI 2.4, last modified on June 7, 2018

After you configure the project, you can start writing test recipes. Your IDE will provide code completion options and other necessary help.

The code snippet below creates a sample test recipe that sends a REST request and checks the received result. You can copy and paste it directly to your class:


public static void main(String[] args) {
    TestRecipe recipe = newTestRecipe()/* Creates a new test recipe. */
        .addStep(restRequest() /* Creates a new test step (REST Request). */
            .get("") /* Specifies the GET method type and a request endpoint. */
            .addQueryParameter("address", "1600+Amphitheatre+Parkway,+Mountain+View,+CA") /* A parameter string. */
                .addQueryParameter("sensor", "false") /* A parameter string. */
                .addAssertion(jsonPathContent("$.results[0].address_components[1].long_name", "Amphitheatre Parkway") /* Create a JsonPath Content assertion. The required assertion parameters are specified in the assertion constructor.*/
                    .allowWildcards() /* Specifies additional assertion parameters. */
        .buildTestRecipe(); /* Important: builds a test recipe for execution. */
    RecipeExecutor recipeExecutor = new RecipeExecutor(Scheme.HTTPS, "localhost", 8443); /* Creates a runner object and specifies a TestServer address for it. */
    recipeExecutor.setCredentials("Tester", "test"); /* Adds basic authentication for requests to TestServer. */
    Execution execution = recipeExecutor.executeRecipe(recipe); /* Runs the test recipe and stores the result to the execution variable for later use. */

If you paste the above-mentioned code to your project, some elements will be highlighted with red. This is because it does not import the required classes. Create the required imports with the help of your IDE (for example, select an unresolved object and press Alt+Enter in IntelliJ IDEA). If you write the code manually, the required classes will be imported automatically.

Your project must have a call to the buildTestRecipe() method after all the test steps are specified. It generates the TestRecipe object, which is then passed to the test runner for execution.

Most methods in the test recipe return a reference to their objects, so you can chain them and make your projects easier to read.

This test recipe is fully functional and can be run. You can see what each line of code does in the comments. The next step of the tutorial describes what methods and settings are necessary to run your recipes.

Tip: You can use the @Test or @Before annotation for the methods that create test recipes to run them as JUnit tests.

Prev     Next

See Also

How to: Typical Tasks
How to: Code-Based Recipes
Creating Java Code-Based Recipes

Highlight search results