Over a million developers have joined DZone.

Graybox Testing — Control Your Dependencies

This post continues the story about functional testing which I referred to in Blackbox Testing Microservices. Read on to learn more.

· DevOps Zone

The DevOps zone is brought to you in partnership with Sonatype Nexus. The Nexus suite helps scale your DevOps delivery with continuous component intelligence integrated into development tools, including Eclipse, IntelliJ, Jenkins, Bamboo, SonarQube and more. Schedule a demo today

This post continues the story about functional testing which I referred to in Blackbox Testing Microservices. In that post we had a clear case of testing a system as blackbox as it had input and expected output but all parts of the system were under our control. There were no external dependencies.

Recently, I worked on a project which had social sign on functionality as one external dependency and Amazon S3 image upload as the other. We could not test the instance of this service through REST API since it internally called these external services. We needed a solution where we could place the external services under our control and give the expected response to our main application when it communicated with them.

Image title

We explored some options and decided to go with WireMock. It is a great little tool where you can define URL patterns, POST body, and URI query parameters; it will intercept those defined patterns and respond with controlled data. It was exactly what we needed. What is even better is the fact that it can work in two modes, you can pull it as a library and the test application will start the WireMock server, or you can download a standalone jar, start the server, and use the library which will send mappings to it through internal call. Since we wanted to have flexibility in our deployment process and have fixed address of a mocked server, which we can configure both in Rest Api application and functional testing application, we decided to go with standalone WireMock server.

So, we started our work. We are running functional tests in an environment dedicated only to that, so we changed the external addresses for Facebook, Twitter, and LinkedIn APIs to point to WireMock server address and port in REST API application. Some changes were easy, where we used direct REST communication, but there were places, such as Twitter, where it was hard to change the URL since we used Twitter API library which wrapped communication, so we had to tweak library configuration for that environment.

The second thing to do was to catch the patterns of all API calls for each social network and to create patterns with WireMock API. We needed to catch the responses as well to get to know the structure that we must return to our application. Such a rule will be sent via the network to the WireMock server so it will know what to intercept and what to give as response to the REST API application.

Image title

The third thing was to install WireMock standalone on a separate instance. We created an Ansible role for that and provisioned a clean instance. Our WireMock server got its own IP address and port.

With this three step approach we created functional tests, which are doing social sign on requests to our application, we are intercepting calls to social networks and returning controlled data so we can test if the flow on our side of application works as expected. The flow with Amazon is similar to what is explained for social networks.

Image title

It was of great help to have these kind of tests in our application since we made sure our part of the integration works, and we added tests which will locally show the errors to developers before deployment to a certain environment. Testing social sign on integration is hard since it must be tested against working application on a social network, which is in some testing environment. This way we are mocking that social network and doing tests early before deployment, which is giving us the confidence to constantly change and improve that part of application. It paid off quite quickly, because we got an additional requirement to pull a profile picture, so we can test drive that, we changed mocked response, changed the application logic, and started tests which proved we did the right thing.

The DevOps zone is brought to you in partnership with Sonatype Nexus. Use the Nexus Suite to automate your software supply chain and ensure you're using the highest quality open source components at every step of the development lifecycle. Get Nexus today

functional testing,java,rest,api,integeration

Published at DZone with permission of Nenad Bozic, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}