1

I have a class that makes a callout, it works fine, but the test class fails

 @isTest
global class FinacleMockResponseGenerator2 implements WebServiceMock {
    global void doInvoke(
        Object stub, 
        Object request, 
        Map<String, Object> response,
        String endpoint, 
        String soapAction, 
        String requestName,
        String responseNS, 
        String responseName, 
        String responseType) {        

            soapFinacle.FinacleServicePort respElement = 
                new soapFinacle.FinacleServicePort();       

            soapFinacle.doBalanceEnquiryResponse  response_x = new soapFinacle.doBalanceEnquiryResponse();
            response_x.ResponseCode = 'test';            
            response_x .AccountName = 'test';
            response_x.AccountNumber = '12345678';
            response_x.AccountStatus = 'test';
            response_x.AvailableBalance = 'test';
            response_x.LedgerBalance = 'test';
            response_x.ResponseDescription = 'SUCCESS';
            response_x.ResponseStatus = 'SUCCESS';    

            soapFinacle.accountBalanceDetails x = new soapFinacle.accountBalanceDetails(); 
            x.accountBalance[0].BalanceType = 'test';
            x.accountBalance[0].BalanceAmountValue = 'test';
            x.accountBalance[0].BalanceAmountCurrency  = 'test';

            response_x.AccountBalanceDetails = x;
            response.put('response_x', response_x);                      
    }
}

TEST CLASS

 @isTest
private class FetchAccountBalancesTest2 {

  @isTest static void testCallOut(){
     Test.setMock(WebServiceMock.class, new FinacleMockResponseGenerator2());

     Account a = new Account();
     a.Name ='Rogue Lamaine';
     a.Tier__c = 'Tier 2';
     a.Ecosystem_Status__c = 'Prospect';
     a.Transregional__c = 'Yes';
     a.Country_Code__c = '207';
     insert a;

     Current_Product_Holding__c x = new Current_Product_Holding__c();
     x.Client__c = a.id;
     x.Account_Number__c = '12345678';
     insert x;

     Test.startTest();
     FetchAccountBalances.getAccountBalance(x.id);
     Test.stopTest();

  }
}
  • In what line does the error occur? – Sander de Jong Feb 15 '18 at 7:39
  • x.accountBalance[0].BalanceType = 'test'; <----- – user2560140 Feb 15 '18 at 7:43
  • Lines where the error is occuring. x.accountBalance[0].BalanceType = 'test'; <--- Mock Response Class and --->FetchAccountBalances.getAccountBalance(x.id); in the test class – user2560140 Feb 15 '18 at 7:45
  • I don't know what the constructor of soapFinacle.accountBalanceDetails looks like, but it seems that it does not initialize accountBalance. – Sander de Jong Feb 15 '18 at 7:53
1

You need to initialize the accountBalance member within the soapFinacle.accountBalanceDetails x.

It will be something like this:

 soapFinacle.accountBalanceDetails x = new 
 soapFinacle.accountBalanceDetails(); 

 // Init the accountBalance property before using it.
 x.accountBalance = new List<WhatEverTypeAccountBalanceIs>();
 x.accountBalance.add(new WhatEverTypeAccountBalanceIs());

 x.accountBalance[0].BalanceType = 'test';
 x.accountBalance[0].BalanceAmountValue = 'test';
 x.accountBalance[0].BalanceAmountCurrency  = 'test';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.