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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. Why Use PowerMock to Mock Private Methods?

Why Use PowerMock to Mock Private Methods?

Roger Hughes user avatar by
Roger Hughes
·
Oct. 25, 11 · Interview
Like (1)
Save
Tweet
Share
51.88K Views

Join the DZone community and get the full member experience.

Join For Free
So far I’ve written a couple of blogs on PowerMock covering some of what I think are its most useful features. Today’s blog takes a look at PowerMock’s ability to mock private methods. At first this struck me as a pretty useless idea. The PowerMock documentation says that you can use it to mock private methods that are called many times during your build process so that you only test them once. My first reaction was does it matter whether or not you execute a piece of code once or a thousand times during a set of unit tests? Usually the answer will be ‘no’ and this feature will be pretty superfluous. However, there is a scenario where is come come in very handy...

Suppose you have a private method that does something really time consuming, for example calculates some statistics by crunching some numbers and it took, for example, 1 minute to work out each result. Calling this method many times during unit testing would seriously increase your build time, breaking Agile’s Ten Minute Build Rule, possibly to the point where building continuously is impractical.

Today’s code sample demonstrates this scenario. The contrived idea is that we’ve written a class called GameStatistics to calculate some statistics for a sports game. This class can cache its statistics only calculating them if they aren’t available, but calculating them takes at least a minute using the private crunchNumbers(...) method. Tests have already been written for the calculation of these stats and we’re happy they work. However, other test scenarios repeatedly call crunchNumbers(...) and it’s seriously affecting the build time. This is where PowerMock’s private method mocking comes in to play...

public class GameStatistics {

  private final boolean noStatsAvailable = true;

  /**
   * A public method
   *
   * @throws InterruptedException
   */
  public String calculateStats() throws InterruptedException {

    if (noStatsAvailable) {
      crunchNumbers();
    }

    return getStatsFromCache();
  }

  /**
   * Calculate some statistic taking a long time.
   */
  private boolean crunchNumbers() throws InterruptedException {

    TimeUnit.SECONDS.sleep(60);
    return true;
  }

  private String getStatsFromCache() {
    return "100%";
  }
}

In the sample code above the crunchNumbers(...) method mimics some kind of long winded calculation by simply sleeping for 60 seconds. This method is called using the public calculateStats(...) method and that's tested using the JUnit code below:
@RunWith(PowerMockRunner.class)
@PrepareForTest(GameStatistics.class)
public class PrivateMethodTest {

  @Test
  public final void testMockPrivateMethod() throws Exception {

    final String methodToTest = "crunchNumbers";
    final String expected = "100%";

    // create a partial mock that can mock out one method */
    GameStatistics instance = createPartialMock(GameStatistics.class, methodToTest);

    expectPrivate(instance, methodToTest).andReturn(true);

    replay(instance);
    final long startTime = System.currentTimeMillis();
    String result = instance.calculateStats();
    final long duration = System.currentTimeMillis() - startTime;
    verify(instance);

    assertEquals(expected, result);
    System.out.println("Time to run test: " + duration + "mS");
  }
}

This is a straight forward PowerMock assisted JUnit test. It uses the usual RunWith(PowerMockRunner.class and a PrepareForTest() call that uses GameStatistics as an argument.

The lines of code that are of interest are:
GameStatistics instance = createPartialMock(GameStatistics.class, methodToTest);

expectPrivate(instance, methodToTest).andReturn(true);
The first of these uses PowerMock’s createPartialMock(...) method to mock a specified part of a class, which in this case is the crunchNumbers(...) method.

The second line of interest is the call to expectPrivate, which sets up the test expectations in the usual way.

Running this test on my machine took 20mS instead of a minute thus significantly reducing our scenario's build time and getting the build process back on track.

 

From http://www.captaindebug.com/2011/10/why-use-powermock-to-mock-private.html

unit test

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Top Authentication Trends to Watch Out for in 2023
  • Spring Cloud: How To Deal With Microservice Configuration (Part 1)
  • Connecting Your Devs' Work to the Business
  • Easy Smart Contract Debugging With Truffle’s Console.log

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: