Rohit Sharma Rohit Sharma - 5 months ago 44
Java Question

How to mock a static method in side a public class using Power mockito?

Hi every one I am trying to mock a static method name mapCreditInfo(UCIPin, creditAssessmentResults) which has two parameters UCIPin and creditAssessmentResults. UCIPin is String type and creditAssessmentResults is List
This method is inside a public class ResponseMapper
type as shown below:

private CreditInfo getAccountByUCI(String audiUser, String UCIPin) throws EnterpriseCustomerVerificationException {
List<CreditAssessmentResult> creditAssessmentResults = creditInfoRepository.getUCISummary(UCIPin, audiUser);
return ResponseMapper.mapCreditInfo(UCIPin, creditAssessmentResults);

Note: getAccountbyUCI method is called inside another public method
name executeCustomerVerification which is in the class

ResponseMapper class

public class ResponseMapper {

public static CreditInfo mapCreditInfo(String UCIPin, List<CreditAssessmentResult> creditAssessmentResults) {
CreditInfo creditInfo = new CreditInfo();


List<AccountCreditInfo> accountCreditInfos = new ArrayList<AccountCreditInfo>();
for (CreditAssessmentResult creditAssessmentResult : creditAssessmentResults) {
AccountCreditInfo accountCreditInfo = new AccountCreditInfo();


return creditInfo;


I have Tried like some portion of my test class as shown below :
Test Class

@PrepareForTest( EnterpriseCustomerVerificationService.class)
public class EnterpriseCustomerVerificationServiceTest {

private EnterpriseCustomerVerificationService enterpriseCustormerVerificationServiceMock ;
public void executeCustomerVerificationTest() throws Exception {
List<ErrorResponse> errorResponses = getErrorResponse();
List<String> mso = new ArrayList<String>();
AddressResponse addressResponse = getAddressResponse();
String experianAuthorization = "experianAuthorization";
String UCIPin = "110019";
String auditUser = "ABC";

CreditInfo credit = getCreditInfo();
CreditCheck creditCheck = getcreditCheck();
EnterpriseCustomerVerificationService spy = PowerMockito.spy(new EnterpriseCustomerVerificationService());

Mockito.when(creditInfoRepository.getUCISummary("110019", "test")).thenReturn(getCreditAssessmentResultList());

Mockito.when(ResponseMapper.mapCreditInfo(UCIPin, getCreditAssessmentResultList())).thenReturn(credit);

CustomerVerification cv = spy

My question is how to mock static mapCreditInfo method using power Mockito ?


Answer Source

Like this ...

public class ATest {

    public void testMockingStatic() {

        // if you want to use specific argument matchers
            uciPin, creditAssessmentResults)

        // or if you want to match on any arguments passed into your static method ...

        // ...


  • @PrepareForTest prepares the class with the static methods which you want to mock
  • PowerMockito.mockStatic mocks all static methods that class
  • You can use standard Mockito when/then constructs to tell the mocked static methods what to return
  • The example above uses these dependencies:
    • org.mockito:mockito-core:2.7.19
    • org.powermock:powermock-module-junit4:1.7.0
    • org.powermock:powermock-api-mockito2:1.7.0

Update 1: based on your updated question which shows your test method ...

My example includes: @PrepareForTest({ResponseMapper.class}) your test method is not preparing ResponseMapper instead it is preparing EnterpriseCustomerVerificationService. It's like you are preparing the class which calls the class which has a static method rather than preparing the class which contains the static method.

I would strongly suggest creating a new test case - just temporarily - which looks like the one I have provided and use that to show yourself how to mock a static method and once you are comfortable with that then work that into your EnterpriseCustomerVerificationServiceTest.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download