Testing Rest-Services with Java

There are enormous tools to test SOA services, such as SOAPUI, PostMan and so on. Those are GUI tools, which can be used for less complex tests. This can be perfectly fine for smoke-testing, basic verification testing and so on. However, when performing complex regression testsuite you often have to ‘code’ the automation suite with Java or any other programming language.

In this blog I will explain my journey with REST service testing. My last assignment was to automate REST services tests using Java TestNG. After experimenting a lot with different Java frameworks, we came to the conclusion that REST-ASSURED framework was by far the best framework for our needs.The Rest-Assured Java framework is a very mature framework and ‘code friendly’ using DSL .

This blog will give you an introduction to the Rest-Assured framework step by step.  We need REST services to demonstrate the framework. There is a pretty cool framework, called wiremock, which will be used in this demo.

Let’s start and create a new Maven project using IntelliJ IDE. If you don’t know how to add a maven project, please follow these four easy steps.

  1. Double click on the pom.xml where we will add the rest-assured and wiredmock dependency. Add the following dependency xml snippet below.  If you get the message ‘maven projects need to be imported’ do please select ‘enable auto-import’.
    <dependencies>
    
     <dependency>
        <groupId>com.github.tomakehurst</groupId>
        <artifactId>wiremock</artifactId>
        <version>2.1.12</version>
     </dependency>
    
     <dependency>
        <groupId>com.jayway.restassured</groupId>
        <artifactId>rest-assured</artifactId>
        <version>2.9.0</version>
     </dependency>
    
    </dependencies>
  2.  Let’s create a test class in the folder ‘src/test/java’, call it ‘TestRestService’. Put this code in the class file.
import com.github.tomakehurst.wiremock.WireMockServer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static com.jayway.restassured.RestAssured.given;
import static org.hamcrest.core.Is.is;


/**
 * Created by TripleQA
 */
public class TestRestService {

    private WireMockServer mockServer;


    @Before
    public void setUpServer(){
        mockServer = new WireMockServer();
        mockServer.start();
        initStubs();
    }

    @Test
    public void exampleGetTest() {


        //This RestAssured:

        given().log().all().

        when().
            get("/demo").
        then().
            statusCode(200).
            body("hello", is("world"));

    }

    private void initStubs(){
        //This is mockServer
        stubFor(get(urlEqualTo("/demo"))
                .willReturn(aResponse()
                        .withStatus(200)
                        .withHeader("Content-Type", "application/json")
                        .withBody("{ \"hello\" : \"world\" }")));
    }


    @After
    public void tearDown(){
        mockServer.stop();
    }

}

let’s have deeper look at the test code ‘exampleGetTest()’. In just one code statement it is calling the ‘get’ HTTP method and validating the content. That’s quite powerful statement without using complex code. As you can see it is using the ‘given-when-then’ specification, which is easy to write accepting tests.

This is an intro in testing REST services, I’ll keep you posted for more complex stuff.

One thought on “Testing Rest-Services with Java

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s