Welcome to the first entry of my Java API Testing blog entry using REST Assured setup. For this blog entry, we will be using Java 11 JDK and Gradle.

Once you have created your Grald project, add the following dependencies:

compile 'org.springframework.boot:spring-boot-starter-web:2.1.5.RELEASE'

testCompile 'org.hamcrest:hamcrest-all:1.3'
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile 'io.rest-assured:rest-assured:3.0.0'
testCompile 'org.springframework.boot:spring-boot-starter-test:2.1.5.RELEASE'

implementation "javax.xml.bind:jaxb-api:2.2.11"
implementation "com.sun.xml.bind:jaxb-core:2.2.11"
implementation "com.sun.xml.bind:jaxb-impl:2.2.11"
implementation "javax.activation:activation:1.1.1"

Now create a new package under the main/java directory called “com.thetestroom”. Inside it create an “Application.java” class. Annotate the class with “@SpringBootApplication” and add in a main method that run’s the application. Your class should look like this:

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Inside the same package let’s create another class called “Controller.java”. In that we will need to annotate the class with “@RestController” and add a method called “sayHello”. The method itself will need to be annotated with “@RequestMapping(“/hi”)”. This will allow Spring to pick up the controller as an endpoint. Your Controller class should look like this:

@RestController
public class Controller {

    @RequestMapping("/hi")
    public String sayHello() {
        return "hi QAShahin";
    }
}

If you now run your application, you should be able to Curl to localhost:8080/hi and that should return “hi QAShahin”.

Now let’s write a test, naviatate to test/java and create a package called “com.thetestroom”. Inside that package create a class called “ControllerTest.java”. Annotate the class with the following annotations:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)

Also add into the test class the following private variable and setup method:

@LocalServerPort
private int port;

@Before
public void setUp() {
    RestAssured.port = port;
}

And now finally the test itself. This library is Rest Assured setup:

@Test
    public void shouldGetStatusOfHi() {
        get("hi").then().body(is("hi QAShahin"));
    }

Your finished test class should look like this:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ControllerTest {

    @LocalServerPort
    private int port;

    @Before
    public void setUp() {
        RestAssured.port = port;
    }
    @Test
    public void shouldGetStatusOfHi() {
        get("hi").then().body(is("hi QAShahin"));
    }
}

Run your test and you should see an instance of your api running and the test passing.

Make sure to checkout my next post on how to use Path Parameters.

Please follow and like us:

Leave a Reply