Saturday 22 July 2017

Mock instance non-void private methods

In this post, I am going to explain how to mock instance non-void methods. PowerMock class provides 'expectPrivate' method to mock instance private methods.
Following snippet is used to mock the instance non-void private method.

StringUtil stringUtil = PowerMock.createPartialMock(StringUtil.class, methodToMock);
String trueStr = "true";
/* Set expectations */
PowerMock.expectPrivate(stringUtil, methodToMock, trueStr).andReturn(true).times(1);

Following is the complete working application.

MethodNotImplementedException.java
package com.sample.model;

public class MethodNotImplementedException extends RuntimeException {
 private static final long serialVersionUID = 1L;

 public MethodNotImplementedException() {
  super();
 }

 public MethodNotImplementedException(String message) {
  super(message);
 }

}

StringUtil.java
package com.sample.util;

import com.sample.model.MethodNotImplementedException;

public class StringUtil {
 public String toUpperAndRepeatStringTwice(String str) {
  String upperCase = str.toUpperCase();
  boolean isLoggingSuccess = sendStringToLogger(str);

  if (isLoggingSuccess) {
   return upperCase + upperCase;
  }

  return null;

 }

 private boolean sendStringToLogger(String str) {
  throw new MethodNotImplementedException();
 }
}


StringUtilTest.java
package com.sample.util;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

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

@RunWith(PowerMockRunner.class)
@PrepareForTest({ StringUtil.class })
public class StringUtilTest {

 @Test
 public void toUpperAndRepeatStringTwice_sendStringToLogger_trueCheck() throws Exception {
  String methodToMock = "sendStringToLogger";

  StringUtil stringUtil = PowerMock.createPartialMock(StringUtil.class, methodToMock);

  String trueStr = "true";

  /* Set expectations */
  PowerMock.expectPrivate(stringUtil, methodToMock, trueStr).andReturn(true).times(1);

  PowerMock.replayAll();

  String result = stringUtil.toUpperAndRepeatStringTwice(trueStr);
  assertEquals(result, "TRUETRUE");

  PowerMock.verifyAll();

 }

 @Test
 public void toUpperAndRepeatStringTwice_sendStringToLogger_falseCheck() throws Exception {
  String methodToMock = "sendStringToLogger";

  StringUtil stringUtil = PowerMock.createPartialMock(StringUtil.class, methodToMock);

  String falseStr = "false";

  /* Set expectations */
  PowerMock.expectPrivate(stringUtil, methodToMock, falseStr).andReturn(false).times(1);

  PowerMock.replayAll();

  String result = stringUtil.toUpperAndRepeatStringTwice(falseStr);
  assertNull(result);

  PowerMock.verifyAll();

 }
}




Previous                                                 Next                                                 Home

No comments:

Post a Comment