Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

Download our Introduction to API Performance Testing and learn why testing your API is just as important as testing your website, and how to start today.

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:

eu.dreamix.rest.RestController.java

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:

eu.dreamix.aspect.SpotifyAspect.java

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.

Aspect

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.

Advice

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

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?

Find scaling and performance issues before your customers do with our Introduction to High-Capacity Load Testing guide.

Topics:
spring aop ,performance ,api management

Published at DZone with permission of Boyko Dimitrov, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

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

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}