Functionality mocking with Spring AOP

Functionality mocking with Spring AOP

Recently our team needed to measure the performance of a client’s application, which heavily depends on third 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 get 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 from

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

Let’s take a look of the following class

As I mentioned at the beginning we are going to mock an existing functionality, so 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

Three terms needs 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 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 the theory in mind, let’s get back to our aspect.

The expression “execution(*” 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 this very useful documentation.

Do you find the article useful? Have you used Spring AOP before and how?

Boyko Dimitrov

Java Developer at Dreamix

More Posts - Website

Follow Me:
TwitterLinkedInGoogle Plus

Do you want more great blogs like this?

Subscribe for Dreamix Blog now!