Spring Boot Test

Maven Dependency

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

Testing Terminology

Unit Tests

Designed to test specific sections of code. Ideal coverage is 70-80%.

Integration Tests

Designed to test behaviors between objects and parts of the overall system. They can include the Spring Context, database and message brokers.

Functional Tests

Testing the running application. Application is live, likely deployed in a known environment.

Spring Boot Test

@SpringBootTest

Annotation that can be specified on a test class that runs Spring Boot based tests. Provides the following features over and above the regular Spring TestContext Framework:

  • Uses 🟢 SpringBootContextLoader as the default ⚪ ContextLoader when no specific @ContextConfiguration(loader=…​) is defined.

  • Automatically searches for a @SpringBootConfiguration when nested @Configuration is not used, and no explicit classes are specified.

  • Allows custom Environment properties to be defined using the properties attribute.

  • Allows application arguments to be defined using the args attribute.

  • Provides support for different webEnvironment modes, including the ability to start a fully running web server listening on a defined or random port.

  • Registers a 🟢 TestRestTemplate and/or ⚪ WebTestClient bean for use in web tests that are using a fully running web server.

@SpringBootTest example
@SpringBootTest
class BookControllerIT {
    @Autowired
    BookController bookController;

    @Test
    void testController() {
        // Assertions
    }
}

How to rollback test?

@Rollback

Used to indicate whether a test-managed transaction should be rolled back after the test method has completed. You can start test-managed transaction with @Transactional.

@Commit can be used as direct replacement for @Rollback(false).

FasterXML Jackson

The Java JSON library.

🟢 ObjectMapper

Provides functionality for reading and writing JSON, either to and from basic POJOs (Plain Old Java Objects), or to and from a general-purpose JSON Tree Model (🟠 JsonNode), as well as related functionality for performing conversions.

You can autowire 🟢 ObjectMapper
@Autowired
ObjectMapper objectMapper;
Reading JSON
Some someObject = objectMapper.readValue(jsonString, Some.class);
List<Some> someList = objectMapper.readValue(jsonArrayString, new TypeReference<List<Some>>(){});
Map<String, Some> someMap = objectMapper.readValue(jsonMapString, new TypeReference<Map<String, Some>>(){});
Writing JSON
String jsonString = objectMapper.writeValueAsString(someObject);

Testing Exceptions

Verify that a particular type of exception is thrown
assertThrows(NotFoundException.class, () -> { (1) (2)
    bookController.updateBookById(UUID.randomUUID(), BeerDTO.builder().build());
});
1 Assertions#assertThrows(Class<T> expectedType, Executable executable) asserts that execution of the supplied executable throws an exception of the expectedType and return the exception.
It not only checks for the type of the thrown exception but also its subclasses, making it suitable for more generalized exception handling tests.
2 ⚪ Executable is a functional interface that can be used to implement any generic block of code that potentially throws a 🟢 Throwable.