Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,6 @@ public void givenUrl_whenVerifiesOddPricesAccuratelyByStatus_thenCorrect() {
.body("odds.findAll { it.status > 0 }.price", hasItems(5.25f, 1.2f));
}

@Test
public void whenRequestedPost_thenCreated() {
with().body(new Odd(5.25f, 1, 13.1f, "X"))
.when()
.request("POST", "/odds/new")
.then()
.statusCode(201);
}

private static String getJson() {
return Util.inputStreamToString(RestAssured2IntegrationTest.class.getResourceAsStream("/odds.json"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,7 @@ public void setup(){
RestAssured.baseURI = "https://api.github.com";
RestAssured.port = 443;
}

@Test
public void whenMeasureResponseTime_thenOK(){
Response response = RestAssured.get("/users/eugenp");
long timeInMS = response.time();
long timeInS = response.timeIn(TimeUnit.SECONDS);

assertEquals(timeInS, timeInMS/1000);
}

@Test
public void whenValidateResponseTime_thenSuccess(){
when().get("/users/eugenp").then().time(lessThan(5000L));
}

@Test
public void whenValidateResponseTimeInSeconds_thenSuccess(){
when().get("/users/eugenp").then().time(lessThan(5L),TimeUnit.SECONDS);
}

//===== parameter

@Test
Expand Down Expand Up @@ -98,42 +79,5 @@ public void whenUseCookieBuilder_thenOK(){
Cookie myCookie = new Cookie.Builder("session_id", "1234").setSecured(true).setComment("session id cookie").build();
given().cookie(myCookie).when().get("/users/eugenp").then().statusCode(200);
}

// ====== request

@Test
public void whenRequestGet_thenOK(){
when().request("GET", "/users/eugenp").then().statusCode(200);
}

@Test
public void whenRequestHead_thenOK(){
when().request("HEAD", "/users/eugenp").then().statusCode(200);
}

//======= log

@Test
public void whenLogRequest_thenOK(){
given().log().all().when().get("/users/eugenp").then().statusCode(200);
}

@Test
public void whenLogResponse_thenOK(){
when().get("/repos/eugenp/tutorials").then().log().body().statusCode(200);
}

@Test
public void whenLogResponseIfErrorOccurred_thenSuccess(){
when().get("/users/eugenp").then().log().ifError();
when().get("/users/eugenp").then().log().ifStatusCodeIsEqualTo(500);
when().get("/users/eugenp").then().log().ifStatusCodeMatches(greaterThan(200));
}

@Test
public void whenLogOnlyIfValidationFailed_thenSuccess(){
when().get("/users/eugenp").then().log().ifValidationFails().statusCode(200);
given().log().ifValidationFails().when().get("/users/eugenp").then().statusCode(200);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.baeldung.restassured;

public class Odd {

float price;
int status;
float ck;
String name;

Odd(float price, int status, float ck, String name) {
this.price = price;
this.status = status;
this.ck = ck;
this.name = name;
}

public float getPrice() {
return price;
}

public void setPrice(float price) {
this.price = price;
}

public int getStatus() {
return status;
}

public void setStatus(int status) {
this.status = status;
}

public float getCk() {
return ck;
}

public void setCk(float ck) {
this.ck = ck;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.baeldung.restassured;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.configureFor;
import static com.github.tomakehurst.wiremock.client.WireMock.containing;
import static com.github.tomakehurst.wiremock.client.WireMock.post;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static io.restassured.RestAssured.get;
import static io.restassured.RestAssured.with;
import static org.hamcrest.Matchers.hasItems;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import com.github.tomakehurst.wiremock.WireMockServer;

import io.restassured.RestAssured;

public class RestAssured2IntegrationTest {
private static WireMockServer wireMockServer;

private static final String ODDS_PATH = "/odds";
private static final String APPLICATION_JSON = "application/json";
private static final String ODDS = getJson();

@BeforeClass
public static void before() {
System.out.println("Setting up!");
final int port = Util.getAvailablePort();
wireMockServer = new WireMockServer(port);
wireMockServer.start();
configureFor("localhost", port);
RestAssured.port = port;
stubFor(com.github.tomakehurst.wiremock.client.WireMock.get(urlEqualTo(ODDS_PATH))
.willReturn(aResponse().withStatus(200)
.withHeader("Content-Type", APPLICATION_JSON)
.withBody(ODDS)));
stubFor(post(urlEqualTo("/odds/new")).withRequestBody(containing("{\"price\":5.25,\"status\":1,\"ck\":13.1,\"name\":\"X\"}"))
.willReturn(aResponse().withStatus(201)));
}

@Test
public void whenRequestedPost_thenCreated() {
with().body(new Odd(5.25f, 1, 13.1f, "X"))
.when()
.request("POST", "/odds/new")
.then()
.statusCode(201);
}

private static String getJson() {
return Util.inputStreamToString(RestAssured2IntegrationTest.class.getResourceAsStream("/odds.json"));
}

@AfterClass
public static void after() {
System.out.println("Running: tearDown");
wireMockServer.stop();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.baeldung.restassured;

import static io.restassured.RestAssured.given;
import static io.restassured.RestAssured.when;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.lessThan;
import static org.junit.Assert.assertEquals;

import java.util.concurrent.TimeUnit;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import io.restassured.RestAssured;
import io.restassured.response.Response;

public class RestAssuredAdvancedLiveTest {

@BeforeEach
public void setup() {
RestAssured.baseURI = "https://api.github.com";
RestAssured.port = 443;
}

// ====== request
@Test
public void whenRequestGet_thenOK() {
when().request("GET", "/users/eugenp")
.then()
.statusCode(200);
}

@Test
public void whenRequestHead_thenOK() {
when().request("HEAD", "/users/eugenp")
.then()
.statusCode(200);
}

@Test
public void whenMeasureResponseTime_thenOK() {
Response response = RestAssured.get("/users/eugenp");
long timeInMS = response.time();
long timeInS = response.timeIn(TimeUnit.SECONDS);

assertEquals(timeInS, timeInMS / 1000);
}

@Test
public void whenValidateResponseTime_thenSuccess() {
when().get("/users/eugenp")
.then()
.time(lessThan(5000L));
}

@Test
public void whenValidateResponseTimeInSeconds_thenSuccess() {
when().get("/users/eugenp")
.then()
.time(lessThan(5L), TimeUnit.SECONDS);
}

//======= log

@Test
public void whenLogRequest_thenOK(){
given().log().all().when().get("/users/eugenp").then().statusCode(200);
}

@Test
public void whenLogResponse_thenOK(){
when().get("/repos/eugenp/tutorials").then().log().body().statusCode(200);
}

@Test
public void whenLogResponseIfErrorOccurred_thenSuccess(){
when().get("/users/eugenp").then().log().ifError();
when().get("/users/eugenp").then().log().ifStatusCodeIsEqualTo(500);
when().get("/users/eugenp").then().log().ifStatusCodeMatches(greaterThan(200));
}

@Test
public void whenLogOnlyIfValidationFailed_thenSuccess(){
when().get("/users/eugenp").then().log().ifValidationFails().statusCode(200);
given().log().ifValidationFails().when().get("/users/eugenp").then().statusCode(200);
}
}
28 changes: 28 additions & 0 deletions testing-modules/rest-assured/src/test/resources/odds.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"odds": [{
"price": 1.30,
"status": 0,
"ck": 12.2,
"name": "1"
},
{
"price": 5.25,
"status": 1,
"ck": 13.1,
"name": "X"
},
{
"price": 2.70,
"status": 0,
"ck": 12.2,
"name": "0"
},
{
"price": 1.20,
"status": 2,
"ck": 13.1,
"name": "2"
}

]
}