Mock Static Methods using Spring Aspects
Join the DZone community and get the full member experience.
Join For FreeI 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.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>
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 :
- 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.
- 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.
- 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() {
AnnotationDrivenStaticComponentMockingControl.aspectOf().playbackInternal();
}
public static void expectReturn(Object retVal) {
AnnotationDrivenStaticComponentMockingControl.aspectOf().expectReturnInternal(retVal);
}
public static void expectThrow(Throwable throwable) {
AnnotationDrivenStaticComponentMockingControl.aspectOf().expectThrowInternal(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.
@Component
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();
@Test
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
- 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.
- 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;
@MockStaticEntityMethods
public class ExampleConfigurationTests {
private ExampleService service = new ExampleService();
@Test
public void testSimpleProperties() throws Exception {
ExampleService.myName();
AnnotationDrivenStaticComponentMockingControl.expectReturn("shekhargulati");
AnnotationDrivenStaticComponentMockingControl.playback();
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.
Opinions expressed by DZone contributors are their own.
Comments