Skip to content

Commit fdf2afa

Browse files
sybraimahKevinGilmore
authored andcommitted
BAEL-1098: Testing a REST API with Karate (eugenp#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
1 parent a6485c4 commit fdf2afa

4 files changed

Lines changed: 100 additions & 0 deletions

File tree

‎rest-testing/pom.xml‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,19 @@
8888
<version>${jbehave.version}</version>
8989
<scope>test</scope>
9090
</dependency>
91+
92+
<dependency>
93+
<groupId>com.intuit.karate</groupId>
94+
<artifactId>karate-apache</artifactId>
95+
<version>${karate.version}</version>
96+
</dependency>
97+
98+
<dependency>
99+
<groupId>com.intuit.karate</groupId>
100+
<artifactId>karate-junit4</artifactId>
101+
<version>${karate.version}</version>
102+
<scope>test</scope>
103+
</dependency>
91104
</dependencies>
92105

93106
<build>
@@ -150,6 +163,7 @@
150163
<rest-assured.version>2.9.0</rest-assured.version>
151164
<cucumber.version>1.2.5</cucumber.version>
152165
<wiremock.version>2.4.1</wiremock.version>
166+
<karate.version>0.6.1</karate.version>
153167

154168
<httpcore.version>4.4.5</httpcore.version>
155169
<httpclient.version>4.5.2</httpclient.version>

rest-testing/src/main/resources/Feature/cucumber.feature renamed to rest-testing/src/main/resources/karate/cucumber.feature

File renamed without changes.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.rest.karate;
2+
3+
import com.github.tomakehurst.wiremock.WireMockServer;
4+
import com.intuit.karate.junit4.Karate;
5+
import cucumber.api.CucumberOptions;
6+
import org.junit.AfterClass;
7+
import org.junit.BeforeClass;
8+
import org.junit.runner.RunWith;
9+
10+
import static com.github.tomakehurst.wiremock.client.WireMock.*;
11+
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
12+
13+
@RunWith(Karate.class)
14+
@CucumberOptions(features = "classpath:karate")
15+
public class KarateUnitTest {
16+
17+
private static final WireMockServer wireMockServer = new WireMockServer();
18+
19+
@BeforeClass
20+
public static void setUp() throws Exception {
21+
wireMockServer.start();
22+
23+
configureFor("localhost", 8080);
24+
stubFor(get(urlEqualTo("/user/get"))
25+
.willReturn(aResponse()
26+
.withStatus(200)
27+
.withHeader("Content-Type", "application/json")
28+
.withBody("{ \"id\": \"1234\", name: \"John Smith\" }")));
29+
stubFor(post(urlEqualTo("/user/create"))
30+
.withHeader("content-type", equalTo("application/json"))
31+
.withRequestBody(containing("id"))
32+
.willReturn(aResponse()
33+
.withStatus(200)
34+
.withHeader("Content-Type", "application/json")
35+
.withBody("{ \"id\": \"1234\", name: \"John Smith\" }")));
36+
37+
}
38+
39+
@AfterClass
40+
public static void tearDown() throws Exception {
41+
wireMockServer.stop();
42+
}
43+
44+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
Feature: Testing a REST API with Karate
2+
3+
Scenario: Testing valid GET endpoint
4+
Given url 'http://localhost:8080/user/get'
5+
When method GET
6+
Then status 200
7+
8+
Scenario: Testing an invalid GET endpoint - 404
9+
Given url 'http://localhost:8080/user/wrong'
10+
When method GET
11+
Then status 404
12+
13+
Scenario: Testing the exact response of a GET endpoint
14+
Given url 'http://localhost:8080/user/get'
15+
When method GET
16+
Then status 200
17+
And match $ == {id:"1234",name:"John Smith"}
18+
19+
Scenario: Testing the exact response field value of a GET endpoint
20+
Given url 'http://localhost:8080/user/get'
21+
When method GET
22+
Then status 200
23+
And match $.id == "1234"
24+
25+
Scenario: Testing that GET response contains specific field
26+
Given url 'http://localhost:8080/user/get'
27+
When method GET
28+
Then status 200
29+
And match $ contains {id:"1234"}
30+
31+
Scenario: Test GET response using markers
32+
Given url 'http://localhost:8080/user/get'
33+
When method GET
34+
Then status 200
35+
And match $ == {id:"#notnull",name:"John Smith"}
36+
37+
Scenario: Testing a POST endpoint with request body
38+
Given url 'http://localhost:8080/user/create'
39+
And request { id: '1234' , name: 'John Smith'}
40+
When method POST
41+
Then status 200
42+
And match $ contains {id:"#notnull"}

0 commit comments

Comments
 (0)