Thursday 15 June 2017

PowerMock, EasyMock : Partial mocking of static methods

In this post, I am going to explain how to mock static methods inside a class partially.

PowerMock class provides 'mockStaticPartial' method to mock the static methods partially. 

Suppose if your class has 10 static methods, but you want to mock only 2 of the static methods, then by using 'mockStaticPartial' method, you can mock only those two static methods.

public static synchronized void mockStaticPartial(Class<?> clazz, String... methodNames)
'clazz' represent the class that contains the static methods that should be mocked and 'methodNames' specifies the names of the methods that should be mocked.

Following step-by-step procedure explain, how to mock some static methods of a class.

Step 1: Prepare the class for testing
@RunWith(PowerMockRunner.class)
@PrepareForTest({ ProductInfo.class })
public class ProductInfoTest {
         ....
         ....
}

Step 2: By using ‘mockStaticPartial’ method of PowerMock class specify the methods to mock.

PowerMock.mockStaticPartial(ProductInfo.class, "getVersion");

Step 3: Set the expectation for mocked methods.
Version version = EasyMock.createMock(Version.class);
EasyMock.expect(version.getReleaseDate()).andReturn("DumyReleaseDate");
EasyMock.expect(version.getVersionNumber()).andReturn("001");

EasyMock.expect(ProductInfo.getVersion()).andReturn(version);

Step 4: Replay the expectations.
EasyMock.replay(version);
PowerMock.replayAll();

Following is the complete working application.

ProductInfo.java
package com.sample.tests;

public class ProductInfo {

 private static String versionNumber = "17.1";
 private static String releaseDate = "04-25-2017";
 
 private static Version version = new Version(versionNumber, releaseDate);

 public static String getProductName() {
  return "Java Tutorial";
 }

 public static Version getVersion() {
  return version;
 }

 public static String getSoftwareRequirements() {
  StringBuilder builder = new StringBuilder();

  return builder.append("Operating System").append(" : ").append("Windows").append("\n").append("RAM")
    .append(" : ").append("4GB").toString();

 }

}


Version.java
package com.sample.tests;

public class Version {
 private String versionNumber;
 private String releaseDate;

 public Version(String versionNumber, String releaseDate) {
  super();
  this.versionNumber = versionNumber;
  this.releaseDate = releaseDate;
 }

 public String getVersionNumber() {
  return versionNumber;
 }

 public String getReleaseDate() {
  return releaseDate;
 }

 public void setVersionNumber(String versionNumber) {
  this.versionNumber = versionNumber;
 }

 public void setReleaseDate(String releaseDate) {
  this.releaseDate = releaseDate;
 }

}


ProductInfoTest.java
package com.sample.tests;

import static org.junit.Assert.*;

import org.easymock.EasyMock;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.api.easymock.PowerMock;

@RunWith(PowerMockRunner.class)
@PrepareForTest({ ProductInfo.class })
public class ProductInfoTest {

 @Test
 public void testProductInfo() {
  PowerMock.mockStaticPartial(ProductInfo.class, "getVersion");

  Version version = EasyMock.createMock(Version.class);
  EasyMock.expect(version.getReleaseDate()).andReturn("DumyReleaseDate");
  EasyMock.expect(version.getVersionNumber()).andReturn("001");

  EasyMock.expect(ProductInfo.getVersion()).andReturn(version);

  EasyMock.replay(version);
  PowerMock.replayAll();

  String productName = ProductInfo.getProductName();
  Version versionReturned = ProductInfo.getVersion();

  assertEquals(versionReturned.getReleaseDate(), "DumyReleaseDate");
  assertEquals(versionReturned.getVersionNumber(), "001");

  assertEquals(productName, "Java Tutorial");
 }
}



Previous                                                 Next                                                 Home

No comments:

Post a Comment