Automated integration testing using Spring test framework of REST API helps to check the functionality, performance and security aspects of API’s.
Nowadays, REST API is extensively used to implement all backend business logic. It is essential to verify the backend functionality implemented using REST API. That is why the automated testing of REST API has gained much deserved popularity. The integration testing of REST API checks the functionality, performance & security aspects of API’s. There are many open source tools and libraries are available to support REST API development and testing. It helps to automate these tests before the GUI development is completed.
As GUI test is difficult to maintain and requires a considerable amount time and efforts, most of the software companies prefer the automated testing of REST API. The automated tests save you time, money and a lot of efforts of maintaining test-cases. REST API test automation includes writing code for making HTTP calls and comparing the server’s actual response with the expected one.
REST API testing involves the following criteria:
- Calling the API with different parameter values
- API boundary value conditions
- Actions performed by API i.e. updating database, calling other API
- Validating Response
Spring TestContext Framework for Automation testing
The Spring TestContext Framework (located in the org.springframework.test.context package) provides generic, annotation-driven unit and integration testing support that is agnostic of the testing framework in use. No need of any tool when you need to automate API testing using Spring Boot, simple coding skills will let you do perfect automation of API testing.
- Spring Boot provides test support by two modules: spring-boot-test which contains core items, and spring-boot-test-autoconfigure which supports auto-configuration for tests.
- Most developers use the spring-boot-starter-test “Starter”, which imports both Spring Boot test modules as well as JUnit, AssertJ, Hamcrest, and a number of other useful libraries.
Advantages of Spring Boot test framework
- A request can be easily prepared and send over the internet
- Assertions in Response data
- Clean code which is easy to be maintained
The spring-boot-starter-test “Starter” (in the test scope) contains the following provided libraries:
- JUnit: The de-facto standard for unit testing Java applications.
- Spring Test & Spring Boot Test: Utilities and integration test support for Spring Boot applications.
- AssertJ: A fluent assertion library.
- Hamcrest: A library of matcher objects (also known as constraints or predicates).
- Mockito: A Java mocking framework.
- JSONassert: An assertion library for JSON.
- JsonPath: XPath for JSON.
These common libraries are very useful in writing tests. Spring boot also allows adding additional test dependencies of your own.
You can write code for unit testing and integration testing using Spring TestContext Framework. With this framework, it is possible to perform integration testing without requiring the deployment of your application or needing to connect to other infrastructure.
Testing with a running server
If you need to start a full running server then use random ports. If you use @SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT), an available port is picked at random each time your test runs.
The @LocalServerPort annotation can be used to inject the actual port used into your test. For convenience, tests that need to make REST calls to the started server can additionally @Autowire a WebTestClient which resolves relative links to the running server and comes with a dedicated API for verifying responses, as shown in the following example:
When running tests, it is sometimes necessary to mock certain components within your application context. For example, you may have a facade over some remote service that is unavailable during development. Mocking can also be useful when you want to simulate failures that might be hard to trigger in a real environment.
Spring Boot includes a @MockBean annotation that can be used to define a Mockito mock for a bean inside your ApplicationContext. You can use the annotation to add new beans or replace a single existing bean definition. The annotation can be used directly on test classes, on fields within your test, or on @Configuration classes and fields. When used on a field, the instance of the created mock is also injected. Mock beans are automatically reset after each test method.
The following example replaces an existing RemoteService bean with a mock implementation: