Over a million developers have joined DZone.

Mock Static Methods using Spring Aspects

DZone's Guide to

Mock Static Methods using Spring Aspects

· Java Zone
Free Resource

Learn how our document data model can map directly to how you program your app, and native database features like secondary indexes, geospatial and text search give you full access to your data. Brought to you in partnership with MongoDB.

I am a Spring framework user for last three years and I have really enjoyed working with Spring. One thing that I am seeing these days is the heavy use of AspectJ in most of SpringSource products. Spring Roo is a RAD tool for Java developers which makes use of AspectJ ITD's for separate compilation units. Spring transaction management and exception translation is also done using Aspectj. There are also numerous other usages of Aspectj in Spring products. In this article, I am going to talk about another cool usage of AspectJ by Spring - Mocking Static Methods.

These days most of the developers write unit test cases and it is very common to use any of the mocking libraries like EasyMock, Mockito etc. to mock the external dependencies of a class.  Using any of these mocking libraries it is very easy to mock calls to other class instance method. But most of these mocking framework does not provide the facility to mock the calls to static methods. Spring provides you the capability to mock static methods by using Spring Aspects library. In order to use this feature you need to add spring-aspects.jar dependency in your pom.xml.



Next thing you need to do is to convert your project in to a AspectJ project. If you are using Eclipse or STS(SpringSource Tool Suite) you can do that by right-click your project -> configure -> convert to AspectJ  project.  STS by default has AspectJ plugin, for eclipse users you need to install AspectJ plugin for above to work. I would recommend using STS for developing Spring based applications.

The two aspects and one annotation that is of interest in spring-aspects.jar are :

  1. AbstractMethodMockingControl : This is an abstract aspect to enable mocking of methods picked out by a pointcut. All the child aspects need to define mockStaticsTestMethod() and methodToMock() pointcuts. The mockStaticTestMethod() pointcut is used to indicate when mocking should be triggered and methodToMock() pointcut is used to define which method invocations to mock.
  2. AnnotationDrivenStaticEntityMockingControl : This is the single implementation of AbstractMethodMockingControl aspect which exists in spring-aspects.jar. This is an annotation-based aspect to use in a test build to enable mocking static methods on Entity classes. In this aspect mockStaticTestMethod() pointcut defines that for classes marked with @MockStaticEntityMethods annotation mocking should be triggered and methodToMock() pointcut defines that all the public static methods in the classes marked with @Entity annotation should be mocked.
  3. MockStaticEntityMethods : Annotation to indicate a test class for whose @Test methods static methods on Entity classes should be mocked.

The AnnotationDrivenStaticEntityMockingControl provide the facility to mock static methods of any class which is marked with @Entity annotation. But usually we would need to mock static method of classes other than marked with @Entity annotation. The only thing we need to do to make it work is to extend AbstractMethodMockingControl aspect and provide definitions for mockStaticsTestMethod() and methodToMock() pointcuts. For example, lets write an aspect which should mock all the public static methods of classes marked with @Component annotation.

package com.shekhar.javalobby;

import org.springframework.mock.staticmock.AbstractMethodMockingControl;
import org.springframework.stereotype.Component;
import org.springframework.mock.staticmock.MockStaticEntityMethods;;

public aspect AnnotationDrivenStaticComponentMockingControl extends
AbstractMethodMockingControl {

public static void playback() {

public static void expectReturn(Object retVal) {

public static void expectThrow(Throwable throwable) {

protected pointcut mockStaticsTestMethod() : execution(public * (@MockStaticEntityMethods *).*(..));

protected pointcut methodToMock() : execution(public static * (@Component *).*(..));

The only difference between AnnotationDrivenStaticEntityMockingControl(comes with spring-aspects.jar) and AnnotationDrivenStaticComponentMockingControl(custom that we have written above) is in methodToMock() pointcut. In methodToMock() pointcut we have specified that it should mock all the static methods in any class marked with @Component annotation.

Now that we have written the custom aspect lets test it. I have created a simple ExampleService with one static method. This is the method which we want to mock.

public class ExampleService implements Service {

* Reads next record from input
public String getMessage() {
return myName();

public static String myName() {
return "shekhar";


This class will return "shekhar" when getMessage() method will be called. Lets test this without mocking

package com.shekhar.javalobby;

import org.junit.Assert;
import org.junit.Test;

public class ExampleConfigurationTests {

private ExampleService service = new ExampleService();

public void testSimpleProperties() throws Exception {
String myName = service.getMessage();
Assert.assertEquals("shekhar", myName);



This test will work fine. Now let's add mocking to this test class. There are two things that we need to do in our test

  1. We need to annotate our test with @MockStaticEntityMethods to indicate that static methods of @Component classes will be mocked. Please note that it is not required to use @MockStaticEntityMethods annotation you can create your own annotation and use that in mockStaticsTestMethod() pointcut.  So, I could have created an annotation called @MockStaticComponentMethods and used that in mockStaticsTestMethod() pointcut. But I just reused the @MockStaticEntityMethods annotation.
  2. In our test methods we need to first invoke the static method which we want to mock so that it gets recorded. Next we need to set our expectation i.e. what should be returned from the mock and finally we need to call the playback method to stop recording mock calls and enter playback state.

To make it more concrete lets apply mocking to the above test

import org.junit.Assert;
import org.junit.Test;
import org.springframework.mock.staticmock.MockStaticEntityMethods;

public class ExampleConfigurationTests {

private ExampleService service = new ExampleService();

public void testSimpleProperties() throws Exception {
String myName = service.getMessage();
Assert.assertEquals("shekhargulati", myName);



As you can see we annotated the test class with @MockStaticEntityMethods annotation and in the test method we first recorded the call (ExampleService.myName()), then we set the expectations, then we did the playback and finally called the actual code.

In this way you can mock the static method of class.

Discover when your data grows or your application performance demands increase, MongoDB Atlas allows you to scale out your deployment with an automated sharding process that ensures zero application downtime. Brought to you in partnership with MongoDB.


Opinions expressed by DZone contributors are their own.


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.


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

{{ parent.tldr }}

{{ parent.urlSource.name }}