HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

REST API integration test

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
restapitestintegration

Problem

We have a (Spring) REST API that I was tasked to write integration tests for. I wrote various ways to test our REST API and then just decided to make 1 integration test class that processes all JSON files that can be submitted in PUT requests against our REST API and also tests security and various GETs and DELETEs. This test class currently makes 586 HTTP (GET, PUT and DELETE) requests generally at or under a total of 30 seconds (about 20 HTTP requests/second). I decided to write my integration test without any 3rd party libs/frameworks after having frustration with Spring, Apache Http Components and other libs. At first I split up my test into several classes but then just decided to put it all into 1 class. I typically write small classes but wanted to experiment with 1 larger class to see what that looked/felt like. This code uses no 3rd party libs and only requires JSON files and a running REST API to process.

What are your thoughts about how I've set this up? Would you despise being assigned to modify this code if you were one of my coworkers? If you think this class should be "split" up, what advantages would be gained?

I developed this in IntelliJ IDEA which is currently reporting no warnings/errors on the class.

```
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.util.*;

// These tests expect the enable assertions flag ("-ea") to be set as a VM option
public final class RestApiIntTests {

private static final String BASIC_AUTH_TOKEN = Base64.getEncoder().encodeToString("sa:sa".getBytes(StandardCharsets.UTF_8));

private static final Map STANDARD_HEADERS = new HashMap() {{
put("Authorization", "Basic " + BASIC_AUTH_TOKEN);
put("Content-Type", "application/json");
put("Accept", "application/json");
}};

private enum HttpVerb {
DELETE, GET, PUT
}

public st

Solution

If you think this class should be "split" up, what advantages would be
gained?

People would be able to read this code in an acceptable time and maybe even modify it, if it is required.

You are asking about splitting code into multiple classes, but I think that you should take a step back and focus on splitting into multiple methods. Your code is almost one big method which does everything. It looks like you feel that it needs to be split – you use comments to mark sections.

Why do I have to read your whole code if I only want to understand what it does? People who work with existing code are mostly interested to understand what code does, not how it does. You need to abstract things. Classes, methods are tools that help to achieve this.

Context

StackExchange Code Review Q#147468, answer score: 2

Revisions (0)

No revisions yet.