3 IntegrationTests
djmil edited this page 2023-07-21 12:39:40 +02:00
This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

It is expected that at this point you've worked thru Home#RESTful API section of a tutorial.

Update `src/test/java/djmil/cashcard/CashCardApplicationTests.java with this Integration Test:

package djmil.cashcard;

import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class CashCardApplicationTests {
    @Autowired
    TestRestTemplate restTemplate;

    @Test
    void shouldReturnACashCardWhenDataIsSaved() {
        ResponseEntity<String> response = restTemplate.getForEntity("/cashcards/99", String.class);

        assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
        
        DocumentContext documentContext = JsonPath.parse(response.getBody());
		Number id = documentContext.read("$.id");
		assertThat(id).isNotNull();
		assertThat(id).isEqualTo(99);
    }
}

Mock an SpringBoot application

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class CashCardApplicationTests {

A @SpringBootTest annotation is a way to start our Spring Boot application and make it available for our tests to perform requests to it.

A way to perform HTTP requests

HTTP helper

class CashCardApplicationTests {
    @Autowired
    TestRestTemplate restTemplate;

TestRestTemplate can be used to make HTTP requests to the locally running (web) application.

Note that while UnitTests#@Autowired annotation is a convenient form of Spring dependency injection ==its best used only in tests==. We'll discuss this in more detail later.

Perform actual Get request

@Test
void shouldReturnACashCardWhenDataIsSaved() {
    ResponseEntity<String> response = restTemplate.getForEntity("/cashcards/99", String.class);

Here we use restTemplate to make an HTTP GET request to the application endpoint /cashcards/99.

restTemplate will return a Get#ResponseEntity, which we've captured in a variable we've named response.

Additional validators

Now, we can (and shall) validate various aspects of expected response message.

HTTP Response Status code

assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);

Response Body

DocumentContext documentContext = JsonPath.parse(response.getBody());

Converts the response String into a JSON-aware object with lots of helper methods.

Number id = documentContext.read("$.id");
assertThat(id).isNotNull();

We expect that when we request a Cash Card with id of 99 a JSON object will be returned with something in the id field. For now assert that the id is not null.

Complex test debugging

To make tests output more verbose, add this section to the build.gradle file:

// This section causes useful test output to go to the terminal.
test {
    testLogging {
        events "passed", "skipped", "failed" //, "standardOut", "standardError"

        showExceptions true
        exceptionFormat "full"
        showCauses true
        showStackTraces true

        // Change to `true` for more verbose test output
        showStandardStreams = false
    }
}

Additional resources