BAEL-1098: Testing a REST API with Karate (#2855)

* BAEL-318: updated activation page

* BAEL-1098: Testing a REST API with Karate

* BAEL-1098: updated classpath on CucumberOptions

* BAEL-1098: updated KarateTest to KarateUnitTest

* BAEL-1098: reverting changes to activation.jsp on spring-webflow

* BAEL-1098: reverting changes to activation.jsp on spring-webflow

* BAEL-1098: updated karate version
This commit is contained in:
sybraimah 2017-10-31 13:05:17 +00:00 committed by KevinGilmore
parent a6485c4326
commit fdf2afaa1f
4 changed files with 100 additions and 0 deletions

View File

@ -88,6 +88,19 @@
<version>${jbehave.version}</version> <version>${jbehave.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>com.intuit.karate</groupId>
<artifactId>karate-apache</artifactId>
<version>${karate.version}</version>
</dependency>
<dependency>
<groupId>com.intuit.karate</groupId>
<artifactId>karate-junit4</artifactId>
<version>${karate.version}</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>
@ -150,6 +163,7 @@
<rest-assured.version>2.9.0</rest-assured.version> <rest-assured.version>2.9.0</rest-assured.version>
<cucumber.version>1.2.5</cucumber.version> <cucumber.version>1.2.5</cucumber.version>
<wiremock.version>2.4.1</wiremock.version> <wiremock.version>2.4.1</wiremock.version>
<karate.version>0.6.1</karate.version>
<httpcore.version>4.4.5</httpcore.version> <httpcore.version>4.4.5</httpcore.version>
<httpclient.version>4.5.2</httpclient.version> <httpclient.version>4.5.2</httpclient.version>

View File

@ -0,0 +1,44 @@
package com.baeldung.rest.karate;
import com.github.tomakehurst.wiremock.WireMockServer;
import com.intuit.karate.junit4.Karate;
import cucumber.api.CucumberOptions;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
@RunWith(Karate.class)
@CucumberOptions(features = "classpath:karate")
public class KarateUnitTest {
private static final WireMockServer wireMockServer = new WireMockServer();
@BeforeClass
public static void setUp() throws Exception {
wireMockServer.start();
configureFor("localhost", 8080);
stubFor(get(urlEqualTo("/user/get"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody("{ \"id\": \"1234\", name: \"John Smith\" }")));
stubFor(post(urlEqualTo("/user/create"))
.withHeader("content-type", equalTo("application/json"))
.withRequestBody(containing("id"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody("{ \"id\": \"1234\", name: \"John Smith\" }")));
}
@AfterClass
public static void tearDown() throws Exception {
wireMockServer.stop();
}
}

View File

@ -0,0 +1,42 @@
Feature: Testing a REST API with Karate
Scenario: Testing valid GET endpoint
Given url 'http://localhost:8080/user/get'
When method GET
Then status 200
Scenario: Testing an invalid GET endpoint - 404
Given url 'http://localhost:8080/user/wrong'
When method GET
Then status 404
Scenario: Testing the exact response of a GET endpoint
Given url 'http://localhost:8080/user/get'
When method GET
Then status 200
And match $ == {id:"1234",name:"John Smith"}
Scenario: Testing the exact response field value of a GET endpoint
Given url 'http://localhost:8080/user/get'
When method GET
Then status 200
And match $.id == "1234"
Scenario: Testing that GET response contains specific field
Given url 'http://localhost:8080/user/get'
When method GET
Then status 200
And match $ contains {id:"1234"}
Scenario: Test GET response using markers
Given url 'http://localhost:8080/user/get'
When method GET
Then status 200
And match $ == {id:"#notnull",name:"John Smith"}
Scenario: Testing a POST endpoint with request body
Given url 'http://localhost:8080/user/create'
And request { id: '1234' , name: 'John Smith'}
When method POST
Then status 200
And match $ contains {id:"#notnull"}