Over a million developers have joined DZone.

Functionality Mocking With Spring AOP

DZone's Guide to

Functionality Mocking With Spring AOP

Learn how to mock the response from third-party API providers to help measure the performance of your codebase with Spring AOP.

· Performance Zone ·
Free Resource

SignalFx is the only real-time cloud monitoring platform for infrastructure, microservices, and applications. The platform collects metrics and traces across every component in your cloud environment, replacing traditional point tools with a single integrated solution that works across the stack.

Recently, our team needed to measure the performance of a client’s application, which heavily depends on third party API providers, such as Spotify, Yelp, and TripAdvisor. Our goal was to mock the response from these providers in an easy way, which will later help us to measure the performance of the code base without concerning the response time from the third party APIs. My immediate enlightenment was to take advantage of the Aspect-Oriented Programming with the Spring AOP project. The idea is to intercept the calls to the external providers and execute a custom method which will fake their response.

As usual, this tutorial can be completed step by step or you can directly clone a simple demo project.

The provided git project is a basic Spring Boot application with included Spring AOP dependency.

Let’s take a look at the following class:


package eu.dreamix.rest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; /** * Created by bdimitrov on 6/6/17. */
@RestController @RequestMapping("/rest/") public class SpotifyController {
 @RequestMapping(value = "/spotify", method = RequestMethod.GET) public ResponseEntity < object width = "300"
 height = "150" > callSpotify() {
  return ResponseEntity.ok("Spotify call");
} < /object>

As I mentioned at the beginning we are going to mock an existing functionality, so SpotifyController.java is just a basic rest controller with one method, the returned object of which will be mocked.

The meaningful work unit regarding our task is in the following class:


package eu.dreamix.aspect;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.ResponseEntity; /** * Created by bdimitrov on 6/6/17. */
@Aspect @Configuration public class SpotifyAspect {
 @Pointcut("execution(* eu.dreamix.rest.SpotifyController.callSpotify(..))") public void inSpotifyController() {}
 @Around("inSpotifyController()") public ResponseEntity mockSpotify() {
  return ResponseEntity.ok("Mocked Spotify call");

Three terms need clarification here: aspect, advice, and pointcut.


The key unit of modularity in Object Oriented Programming is the class, whereas in Aspect Oriented Programming the unit of modularity is the aspect. For example, a logging module would be called AOP aspect for logging. An application can have any number of aspects depending on the requirement.


This is the actual action to be taken either before or after the method execution. This is an actual piece of code that is invoked during the program execution. There is a third type of advice which we are going to use in our demo. It is the Around advice.


Pointcut defines at what point in the application the associated advice should be applied. You can specify pointcuts using expressions or patterns. Taking this theory in mind, let’s get back to our aspect.

The expression “execution(* eu.dreamix.rest.SpotifyController.callSpotify(..))” in the @Pointcut declaration tells the framework to catch the execution of a method with name callSpotify in a specified package regarding the return type(*) and method parameters(…). We can define various expressions according to our needs. For example, * *.callSpotify(…) will catch all methods in all classes with name callSpotify.

The method marked with @Around annotation is our advice. This means that the real execution of the callSpotify method will be skipped and mockSpotify() method will be called instead. Other advices are @Before and @After which respectively will be called before or after the pointcut defined method and will not skip the method execution.

If you want to get deeper into  Spring AOP, you can check out this very useful documentation.

Did you find this article useful? Have you used Spring AOP before? How?

SignalFx is built on a massively scalable streaming architecture that applies advanced predictive analytics for real-time problem detection. With its NoSample™ distributed tracing capabilities, SignalFx reliably monitors all transactions across microservices, accurately identifying all anomalies. And through data-science-powered directed troubleshooting SignalFx guides the operator to find the root cause of issues in seconds.

spring aop ,performance ,api management

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}