DEV Community

Luiz Eduardo Martins for Mercedes-Benz.io

Posted on • Updated on • Originally published at mercedes-benz.io

API Testing with Java and Spring Boot Test - Part 1: The basic setup

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

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. 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 to organize, structure and be the foundation of the project.

Setting up the project

let's go gif

Step 1: Create the project

We choose Maven 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 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:

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.

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:

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.

@ExtendWith(SpringExtension.class)
@SpringBootTest
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
Enter fullscreen mode Exit fullscreen mode

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

private final YourApiService yourApiService;

public ApiTest(YourApiService yourApiService) {

    this.yourApiService = yourApiService;
}
Enter fullscreen mode Exit fullscreen mode

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.

@Test
public void testCreateUser() throws JsonProcessingException {

    ObjectMapper mapper = new ObjectMapper();

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

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:

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!

Top comments (0)