Skip to content

Instantly share code, notes, and snippets.

@chrisvasqm
Created March 26, 2024 03:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisvasqm/7c299531fcc7222b3ef76870e2573f1f to your computer and use it in GitHub Desktop.
Save chrisvasqm/7c299531fcc7222b3ef76870e2573f1f to your computer and use it in GitHub Desktop.
API Testing with Java + RestAssured + TestNG + Truth
package org.example.services;
import io.restassured.RestAssured;
import java.util.List;
public class APIClient<T> {
private final Class<T> type;
private final String baseUrl;
private final String endpoint;
private final String CONTENT_TYPE = "application/json";
public APIClient(Class<T> type, String endpoint) {
this.type = type;
this.baseUrl = "https://jsonplaceholder.typicode.com";
this.endpoint = endpoint;
}
public List<T> getAll() {
return RestAssured
.get( baseUrl + endpoint)
.getBody()
.jsonPath()
.getList(".", type);
}
public T create(T todo) {
return RestAssured
.given()
.contentType(CONTENT_TYPE)
.body(todo)
.post(baseUrl + endpoint)
.getBody()
.jsonPath()
.getObject(".", type);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>java-test-api</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>22</maven.compiler.source>
<maven.compiler.target>22</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>5.4.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<version>1.4.2</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>
</dependencies>
</project>
package org.example.models;
public class Todo {
private int id;
private int userId;
private String title;
private boolean completed;
public Todo(int id, int userId, String title) {
this.id = id;
this.userId = userId;
this.title = title;
this.completed = false;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public boolean isCompleted() {
return completed;
}
public void setCompleted(boolean completed) {
this.completed = completed;
}
@Override
public String toString() {
return STR."Todo{id=\{id}, userId=\{userId}, title='\{title}\{'\''}, completed=\{completed}\{'}'}";
}
}
package org.example.services;
import org.example.models.Todo;
import java.util.List;
public class TodoService {
private final APIClient<Todo> client;
public TodoService() {
this.client = new APIClient<>(Todo.class, "/todos");
}
public List<Todo> getAll() {
return client.getAll();
}
public Todo create(Todo todo) {
return client.create(todo);
}
}
import org.example.models.Todo;
import org.example.services.TodoService;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import static com.google.common.truth.Truth.assertThat;
public class TodoTests {
private TodoService service;
@BeforeClass
public void setUp() {
service = new TodoService();
}
@Test
public void getAll_ReturnsTodos() {
var todos = service.getAll();
assertThat(todos).isNotEmpty();
}
@Test
public void create_SampleTodo_ReturnsStatusCreated() {
var todo = new Todo(0, 1, "Titulito");
var newTodo = service.create(todo);
assertThat(newTodo).isNotNull();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment