DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How to Migrate From JUnit 4 to JUnit 5 Step by Step
  • Spring Microservice Application Resilience: The Role of @Transactional in Preventing Connection Leaks
  • Comprehensive Guide to Unit Testing Spring AOP Aspects
  • Mastering Spring: Synchronizing @Transactional and @Async Annotations With Various Propagation Strategies

Trending

  • Streamlining Event Data in Event-Driven Ansible
  • Agentic AI for Automated Application Security and Vulnerability Management
  • How Trustworthy Is Big Data?
  • Doris: Unifying SQL Dialects for a Seamless Data Query Ecosystem
  1. DZone
  2. Coding
  3. Frameworks
  4. Mock Static Methods using Spring Aspects

Mock Static Methods using Spring Aspects

By 
Shekhar Gulati user avatar
Shekhar Gulati
·
Jan. 25, 11 · Interview
Likes (1)
Comment
Save
Tweet
Share
22.1K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Spring Framework Aspect (computer programming) unit test Annotation AspectJ

Opinions expressed by DZone contributors are their own.

Related

  • How to Migrate From JUnit 4 to JUnit 5 Step by Step
  • Spring Microservice Application Resilience: The Role of @Transactional in Preventing Connection Leaks
  • Comprehensive Guide to Unit Testing Spring AOP Aspects
  • Mastering Spring: Synchronizing @Transactional and @Async Annotations With Various Propagation Strategies

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!