Rest Assured is a Java library used for testing RESTful APIs. Before we start using Rest Assured, we need to set it up in our project. In this post, we will learn how to set up Rest Assured in a Java project. There are different ways to set up Rest Assured, but we will be using Maven, a popular build automation tool for Java projects. If you are not familiar with Maven, you can learn more about it here: https://maven.apache.org/what-is-maven.html.
Step 1: Create a Maven project
To get started, we need to create a new Maven project. You can create a Maven project using an IDE like Eclipse or IntelliJ IDEA, or you can create a Maven project using the command line. If you are using the command line, you can use the following command.
mvn archetype:generate -DgroupId=com.example -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
This command will create a new Maven project with the following details:
- groupId: com.example
- artifactId: my-app
You can change the groupId and artifactId according to your project requirements.
Step 2: Add Rest Assured dependency
Once you have created a new Maven project, you need to add Rest Assured dependency in the pom.xml file. Open the pom.xml file and add the following dependency:
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>4.3.3</version>
</dependency>
This dependency will download the Rest Assured library and its dependencies.
Step 3: Write your first Rest Assured test
Now that we have set up Rest Assured in our project, we can start writing our first Rest Assured test. Create a new Java class in the src/test/java folder and name it TestRestAssured. Add the following code to the class:
import io.restassured.RestAssured;
import org.junit.Test;
public class TestRestAssured {
@Test
public void testStatusCode() {
RestAssured.given()
.get("https://jsonplaceholder.typicode.com/posts")
.then()
.statusCode(200);
}
}
This code sends a GET request to https://jsonplaceholder.typicode.com/posts and checks if the response status code is 200. You can run this test using a test runner like JUnit.
That's it! You have now set up Rest Assured in your project and written your first Rest Assured test.
Comments
Post a Comment