'Consume the REST API of another Spring Boot project locally

I am building a microservice architecture with different Spring Boot projects and two different repositories for each one of them:

  • One application is called project-api-1
  • The other one project-api-2.

Now, within the JUnit tests of project-api-2 I need to test some functionality that requires making a REST call to the API exposed by project-api-1.

Is there a way to link these projects together for testing purposes, in such a way that when I run the tests for project-api-2 I can also start the project-api-1 application at runtime, without having to create a mock API of project-api-1 from scratch within project-api-2?

What is the best practice here?



Solution 1:[1]

I ended up solving this problem by running a multi-module Maven project, and importing the REST API project with <scope>test</scope>.

In my tests, I then start the Spring Boot application with:

new SpringApplicationBuilder(RestApplication.class)
  .profiles("test")
  .run();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mark