API testing is crucial in software development, allowing you to validate the functionality, reliability, and performance of your applications. REST Assured is a popular tool that makes it easy to automate testing against RESTful APIs. This post will cover how to set up the REST Assured framework in your Java project, along with examples to get you started on your API testing journey.
Before diving into the setup, ensure you have the following prerequisites:
To start, create a new Maven project. Here’s how to do it in IntelliJ IDEA:
com.example
) and ArtifactId (e.g., RestAssuredDemo
), then click Finish.Alternatively, if you prefer the command line, you can run:
mvn archetype:generate -DgroupId=com.example -DartifactId=RestAssuredDemo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
In your newly created Maven project, open the pom.xml
file and add the following dependencies under the <dependencies>
section:
<dependencies> <dependency> <groupId>io.rest-assured</groupId> <artifactId>rest-assured</artifactId> <version>5.4.0</version> <scope>test</scope> </dependency> <dependency> <groupId>org.hamcrest</groupId> <artifactId>hamcrest</artifactId> <version>2.2</version> </dependency> </dependencies>
Make sure to save the file, and Maven will automatically download the required libraries.
REST Assured requires configuration for specific settings. In your main Java class or test class, you can set the base URI and other configurations.
import io.restassured.RestAssured; import org.junit.jupiter.api.BeforeAll; public class ApiTest { @BeforeAll public static void setup() { RestAssured.baseURI = "https://jsonplaceholder.typicode.com"; // Sample API } }
You can also set other configurations like authentication, if required:
RestAssured.authentication = RestAssured.basic("username", "password");
Now that we have set up the environment, let's write a basic test case using JUnit 5. Create a new Java class called ApiTest
:
import io.restassured.RestAssured; import org.junit.jupiter.api.Test; import static io.restassured.RestAssured.*; import static org.hamcrest.Matchers.*; public class ApiTest { @BeforeAll public static void setup() { RestAssured.baseURI = "https://jsonplaceholder.typicode.com"; } @Test public void testGetPosts() { given() .when() .get("/posts") .then() .statusCode(200) .body("[0].title", notNullValue()); } }
get()
, post()
, put()
, etc.To run your tests, you can use your IDE’s built-in test runner or run the following command in the terminal:
mvn test
You should see output indicating the tests have run and any assertions made.
@ParameterizedTest
..log().all()
to your request for complete request/response logs.given() .log().all() .when() .get("/posts") .then() .log().all();
With this setup, you are now ready to dive deeper into API testing using REST Assured. The framework offers a robust and clean way to validate RESTful services, making your testing processes both efficient and effective. As you progress in your learning journey, explore more functionalities that REST Assured provides, and apply them to your real-world projects for better test coverage and reliability.
21/09/2024 | API Testing
18/09/2024 | API Testing
26/10/2024 | API Testing
26/10/2024 | API Testing
21/09/2024 | API Testing
18/09/2024 | API Testing
26/10/2024 | API Testing
26/10/2024 | API Testing
18/09/2024 | API Testing
26/10/2024 | API Testing