Shekhar Gulati is a Java Consultant with over 5 years experience.He is currently working with Xebia India an Agile Software Development company.The postings on this site and on his blog are his own and do not necessarily represent opinion of his employer. His own blog is at http://whyjava.wordpress.com/ and you can follow him on twitter here. Shekhar has posted 25 posts at DZone. View Full User Profile

Mock Static Methods using Spring Aspects

01.25.2011
| 10451 views |
  • submit to reddit

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.

<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 :

  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() {
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

  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;

@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.

Published at DZone with permission of its author, Shekhar Gulati.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Mikhail Stepura replied on Tue, 2011/01/25 - 2:19am

Why just don't use the Powermock? http://code.google.com/p/powermock/wiki/MockStatic

Nicolas Frankel replied on Tue, 2011/01/25 - 5:55am

Why use static methods at all? If you use Spring, beans are singleton by default. The only pertinent use case is mocking a static method from a third-party library, which is not apparent from your example.

What's the point in designing static methods? They make your code far more complext to test, and you have to use powerful tools in order to still test them. I wish good luck to the poor developer who has to do the maintenance of your projects, once you're gone...

Shekhar Gulati replied on Tue, 2011/01/25 - 7:22am in response to: Nicolas Frankel

Hello Nicolas,
Thanks for your comment. I agree with you that it is difficult to test static methods and normally you should not design classes with static methods. But I still think there are lot of use cases for static methods eg. utility methods. I learnt this from Spring Roo. As you might know in Spring Roo domain objects are not anemic. In Spring Roo whenever you add a finder using finder command the finder method that's get added is static. To make testing of these static methods they provided the capability to mock static methods.


Again I am not saying you should use it but I think it is a good thing to know.

Thanks
Shekhar

Rogerio Liesenfeld replied on Tue, 2011/01/25 - 9:46am

Mocking static methods (and just about anything else) is easy. For example, you could write the following equivalent test with JMockit:
import org.junit.*;
import mockit.*;

public class ExampleConfigurationTests
{
    private ExampleService service = new ExampleService();

    @Test
    public void testSimpleProperties()
    {
        new Expectations(ExampleService.class) {{
            ExampleService.myName(); result = "shekhargulati";
        }}

        String myName = service.getMessage();
        Assert.assertEquals("shekhargulati", myName);
    }
}
Static methods can be very useful. For example, to implement a static persistence facade, which in my experience is a much better design than DAOs or Repositories.

Daniel Serodio replied on Tue, 2012/10/16 - 1:25pm in response to: Rogerio Liesenfeld

Rogerio, got any examples (or blog posts) about this static persistence facade you mention?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.