API Testing with Java and Spring Boot Test - Part 1: The basic setup
Tags: beginners, tutorial, java, restassured
Published: 2022-12-16 14:12:08

At Mercedes-benz.io (MB.io), we collaborate as multiple multi-disciplinary teams (nothing new to a Scrum-based organization). 

I'm part of one of those teams, responsible for a Java-based microservice. Since this microservice sends data to a back-office application, we need to test the APIs provided by it.

With that said, we had the challenge to build a new API test framework from scratch. 

In this series of articles we'll show: 
- How we choose the tools
- The process of creating and improving the test framework
- Pipeline configuration
- Test report

![Eddie Murphy's OK gesture](https://media.giphy.com/media/3oEjI5VtIhHvK37WYo/giphy.gif)

## Choosing the language and framework

The main reason why we went for a Java-based framework is that the background of our team is Java and the microservice itself is written in this language. Our team is composed of Java developers, so they can contribute to building the right solution for our team and also maintain the code base of the test repository in case it's needed.

The test framework we've chosen to be the base of our solution was [Rest Assured.io](https://rest-assured.io/). The reason behind it is that rest assured is already used in several projects within our tribe at MB.io and is also widely used and maintained in the community.

We also added [Spring Boot](https://spring.io/) to organize, structure and be the foundation of the project.

## Setting up the project

![let's go gif](https://media.giphy.com/media/fKujfcYtnod4R6soBF/giphy.gif)

### Step 1: Create the project

We choose [Maven](https://maven.apache.org/) as our dependencies manager. Now, the first thing to do is to add the dependencies we need in our project.

**Tip**: You can use [the spring boot initializer](https://start.spring.io/) to get the basic `pom.xml` file with the spring initial setup.

After the initial setup, we need to add the dependencies for the rest-assured test framework and other things we'll use to make our lives easier. 

The `pom.xml` file should be something like that:

{% embed https://gist.github.com/luuizeduardo/9b55d6d6405b6c6588cab243b41cd2b2.js %}

With this, we should be able to start organizing our project.

### Step 2: Changing the `Main` class

The `Main` class should be changed to a `SpringBootApplication`. And the main method must be configured to run as a `SpringApplication`.

{% embed https://gist.github.com/luuizeduardo/291889b3b2fd67d398bdc59ddfb8a339.js %}

### Step 3: Create a `Service` to manage your API

To abstract access and configure the requests in one single place, we can create a new `Service` and take advantage of it.

Here is the place to set the proper configuration of the requests.

Let's create a new method here to abstract the use of a post request. In this post request, we'll provide the URL and the JSON body as parameters, so the file will be something like this:

{% embed https://gist.github.com/luuizeduardo/e8b8390d1135366c671149fb6d58ab56.js %}

**Note:** We'll return the full response to be able to validate what we want within the test itself.

As you can see in the file above, we also take advantage of the built-in `RequestSpecification` that Rest-assured has to set the ***baseURI*** and ***basePath*** for this service. This is a smart way to configure your service because if you have more than one service in our test framework, each of them can have its setup and host.

### Step 4: Add a test case

First things first, let's add the proper annotations to be a spring boot JUnit 5 test class.

```java
@ExtendWith(SpringExtension.class)
@SpringBootTest
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
```

After that, let's add a constructor method and assign the `Service` to be used in our test as a class variable.

```java
private final YourApiService yourApiService;

public ApiTest(YourApiService yourApiService) {

    this.yourApiService = yourApiService;
}
```

Now we are good to start adding the test cases here. Let's do that.

The `postRequest` method expects two parameters: 
- the endpoint we want to send the data as a String;
- the request body as a JsonNode. 

The first thing we want to do is create an object to send in the request body of our request. We'll take advantage of the `jackson-databind` library to help us with the object mapping.

```java
@Test
public void testCreateUser() throws JsonProcessingException {

    ObjectMapper mapper = new ObjectMapper();

    String body = "{\"name\": \"Luiz Eduardo\", \"job\": \"Senior QA Engineer\"}";
    JsonNode requestBody = mapper.readTree(body);
}
```

Now, we need to make the request and validate what we want. Let's add that to our test case. The final results should be something like that:

{% embed https://gist.github.com/luuizeduardo/cda1c63b32fb09136e0b73722edb4d36.js %}

**Note:** Bear in mind that this is just the first iteration, we'll improve the code to keep the responsibilities within the respective classes.

What we've seen so far:

- The project's basic setup
- How to structure the project
- How to abstract the requests in the service layer
- The first functional test case

This is the end of part 1 of this series of articles. The next part will cover:
- A short refactor on the object mapping
- Improve the response validation
- Property files

See you soon!