+ Start a Discussion
Linda 98Linda 98 

help with test class please

I am trying to get increase code coverage for this code which is part of unmanaged package.
I could get onlyy 12 lines out of 51 .Which is casuing for my other code to fail in deployment.

Please help me.

this is the apex class
public class CreateStatementPDFConroller{
public String todayDate {get;set;}
public  String ids {get;set;}
public Map<String,accountInDirectPrice> accountInDirectPriceMap{get;set;}

public Map< Account , List< AcctSeed__Billing__c > > accountBillingMap {get;set;}
    public CreateStatementPDFConroller(){
        
        accountInDirectPriceMap = new Map<String,accountInDirectPrice>();
        
        List<Account> accList = [Select Id,Name,Account_Code__c,AcctSeed__Billing_Contact__r.Email FROM Account WHERE Balance_Remaining__c > 0 AND RecordType.Name =: Label.SubAgentAccount];
        ids = '';                                             
        for(Account acc:accList) {
            ids += acc.Id + ',';
        }
        
        todayDate = Datetime.now().format('MM/dd/yyyy');
        accountBillingMap = New Map< Account , List< AcctSeed__Billing__c > >();
        // parameter ids 
             //ids = ApexPages.currentPage().getParameters().get('id');
        if( String.IsNotBlank( ids ) ){
            List< String > idLst = ids.split(','); 
            
            Map< Id , Account > accountMap = New Map< Id , Account >([SELECT Id, Account_Code__c, Name, BillingStreet, BillingCity, BillingState, BillingPostalCode, BillingCountry 
                                  FROM Account 
                                  WHERE Id IN : idLst]);
            // fill account to map                       
            for( Account ac : accountMap.values() ){
                accountBillingMap.put(ac , New List< AcctSeed__Billing__c >());
                accountInDirectPrice accval = new accountInDirectPrice();
                accountInDirectPriceMap.put(ac.id,accval);
            }   
                               
            List< AcctSeed__Billing__c > billing = [SELECT Id ,AcctSeed__Age__c ,Name, AcctSeed__Customer__r.Name,Insured__c,AcctSeed__Status__c,AcctSeed__Proprietary_Billing_Number__c,AcctSeed__Date__c,Policy__r.Billing_Company__r.Name,Policy__r.Effective_Date__c,Policy__r.Name,AcctSeed__Due_Date2__c,Policy__r.Name_Insured__c,Policy__r.Policy_Type__c,Policy__r.Status_Type__c,
                                          Sub_Agent_Percent__c,Sub_Agent_Commission_Total__c,Transaction__r.Transaction_Amount__c,Transaction__r.Account__r.Name,AcctSeed__Total__c,Transaction__r.Name,Amount_Due__c,Billing_Type__c,AcctSeed__Balance__c,AcctSeed__Received_Amount__c  FROM AcctSeed__Billing__c 
                                          WHERE AcctSeed__Customer__c IN : idLst  ];
            if( !billing.IsEmpty() ){
                for( AcctSeed__Billing__c ab : billing ){
                    if( accountMap.containsKey(ab.AcctSeed__Customer__c) && accountBillingMap.containsKey(accountMap.get(ab.AcctSeed__Customer__c) ) ){
                       
                        //ab.AcctSeed__Date__c = date.ValueOf(DateTime.newInstance(ab.AcctSeed__Date__c.month(),ab.AcctSeed__Date__c.day(),ab.AcctSeed__Date__c.year()).format('d-MM-YYYY'));
                        accountBillingMap.get(accountMap.get(ab.AcctSeed__Customer__c)).add(ab);
                    }
                    
                    if(accountMap.containsKey(ab.AcctSeed__Customer__c)){
                            
                            if(accountInDirectPriceMap.containsKey(ab.AcctSeed__Customer__c) && ab.AcctSeed__Age__c != null && ab.AcctSeed__Age__c != '' && ab.AcctSeed__Age__c != 'Closed'){
                                
                                accountInDirectPrice accval = accountInDirectPriceMap.get(ab.AcctSeed__Customer__c);
                                if(ab.AcctSeed__Age__c == '0-Current'){
                                    
                                    accval.currentPrice += ab.AcctSeed__Balance__c;
                                
                                } else if(ab.AcctSeed__Age__c == '1-30 Days'){
                                    
                                    accval.to30Days += ab.AcctSeed__Balance__c;
                                    
                                } else if(ab.AcctSeed__Age__c == '31-60 Days'){
                                    
                                    accval.to60Days += ab.AcctSeed__Balance__c;
                                    
                                } else if(ab.AcctSeed__Age__c == '61-90 Days'){
                                    
                                    accval.to90Days += ab.AcctSeed__Balance__c;
                                    
                                } else if(ab.AcctSeed__Age__c == 'Over 90 Days'){
                                                                    
                                    accval.over90days += ab.AcctSeed__Balance__c;
                                }
                                
                                accountInDirectPriceMap.put(ab.AcctSeed__Customer__c,accval);
                                
                                
                            }  
                     }
                }
            }
        }        
    }
    
    public class accountInDirectPrice{
      
      public Decimal currentPrice {get;set;}
      public Decimal to30Days{get;set;}
      public Decimal to60days{get;set;}
      public Decimal to90days{get;set;}
      public Decimal over90days{get;set;}
      
      public accountInDirectPrice(){
          
          currentPrice = 0;
          to30Days = 0;
          to60Days = 0;
          to90Days = 0;
          over90days = 0;
      }
    }
}

test class
 
@isTest
public class CreateStatementPDFConrollerTest {
    @isTest
    public static void unitTest(){
        List<Account> accountLst = new List<Account>();
        Account accObj1 = new Account(Name='test',Balance_Remaining__c=20);
        accountLst.add(accObj1);
        Account accObj2 = new Account(Name = 'test',Balance_Remaining__c=23);
        accountLst.add(accObj2);
        insert accountLst;

        RecordType RecordType1 = [SELECT Id from RecordType where name='General Liability' and sobjecttype = 'Policy__c'];
        
        Policy__c policyObj = new Policy__c();
        policyObj.recordTypeId = RecordType1.Id;
        policyObj.Account__c = accountLst[0].Id;
        policyObj.Name = 'test';
        policyObj.Agency_Commission__c = 10.1;
        policyObj.Billing_Type__c = 'Agency Bill';
        policyObj.Billing_Company__c = accountLst[1].Id;
        insert policyObj;
        
        Transaction__c transObj = new Transaction__c();
        transObj.Account__c = accountLst[0].Id;
        transObj.Policy_Name__c = policyObj.Id;
        transObj.Transaction_Amount__c = 10;
        transObj.Tran_Code__c = 'Audit Premium- Add';
        transObj.Client_Type__c='Direct Insured';
        insert transObj;
        
        AcctSeed__Accounting_Period__c accountingPeriod = new AcctSeed__Accounting_Period__c();
        accountingPeriod.Name = '2016-12';
        accountingPeriod.AcctSeed__Start_Date__c = System.today().addDays(792);
        accountingPeriod.AcctSeed__End_Date__c = System.today().addDays(7888);
        accountingPeriod.AcctSeed__Status__c = 'Open';
        insert accountingPeriod;
        
        AcctSeed__Billing_Format__c billingformatObj = new AcctSeed__Billing_Format__c();
        billingformatObj.name = 'test';
        billingformatObj.AcctSeed__Type__c = 'Billing';
        billingformatObj.AcctSeed__Visualforce_PDF_Page__c = 'BillingActivityStatementPDF';
        billingformatObj.AcctSeed__Default_Email_Template__c = 'Activity_Statement_Email_Template';
        insert billingformatObj;
        
        AcctSeed__Billing__c billingObj = new AcctSeed__Billing__c();
        billingObj.AcctSeed__Status__c = 'In Process';
        billingObj.AcctSeed__Customer__c = accountLst[1].Id;
        billingObj.Transaction__c = transObj.Id;
        billingObj.Policy__c = policyObj.Id;
        billingObj.AcctSeed__Date__c = System.today().adddays(76);
        billingObj.AcctSeed__Accounting_Period__c = accountingPeriod.Id;
        billingObj.AcctSeed__Billing_Format__c = billingformatObj.Id;
        insert billingObj;
        
        ApexPages.currentPage().getParameters().put('id',accountLst[0].id);
        CreateStatementPDFConroller controllerObj = new 
        CreateStatementPDFConroller();
        
       

    }
}

 
Best Answer chosen by Linda 98
v varaprasadv varaprasad
Hi Linda,

Add record type to account and check once : 
 
Id devRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Label.SubAgentAccount').getRecordTypeId();

Account accObj1 = new Account(Name='test',Balance_Remaining__c=20,Recordtypeid=devRecordTypeId);
        accountLst.add(accObj1);
        Account accObj2 = new Account(Name = 'test',Balance_Remaining__c=23,Recordtypeid=devRecordTypeId);
        accountLst.add(accObj2)

CreateStatementPDFConroller controllerObj = new CreateStatementPDFConroller(); 
CreateStatementPDFConroller.accountInDirectPrice act = new CreateStatementPDFConroller.accountInDirectPrice();

Hope this helps you!

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com

 

All Answers

v varaprasadv varaprasad
Hi Linda,

Add record type to account and check once : 
 
Id devRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Label.SubAgentAccount').getRecordTypeId();

Account accObj1 = new Account(Name='test',Balance_Remaining__c=20,Recordtypeid=devRecordTypeId);
        accountLst.add(accObj1);
        Account accObj2 = new Account(Name = 'test',Balance_Remaining__c=23,Recordtypeid=devRecordTypeId);
        accountLst.add(accObj2)

CreateStatementPDFConroller controllerObj = new CreateStatementPDFConroller(); 
CreateStatementPDFConroller.accountInDirectPrice act = new CreateStatementPDFConroller.accountInDirectPrice();

Hope this helps you!

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com

 
This was selected as the best answer
Linda 98Linda 98
I tried adding record type but it didnt work the way mentioned above.A normal soql to recordtype record had worked.And yes i missed calling methods of my class.It worked.thank you.