+ Start a Discussion
Priya134Priya134 

test class help - only 58%

public with sharing class Prefundscreen{

    public String oppId{get;set;}
    //public String accId{get;set;}
    //public String vaccName{get;set;}
    public List<Prefund__c> prefundList{get;set;}
    public List<Vendor__c> vendorList{get;set;}  
    public List<Opportunity> oppList{get;set;}  
    public List<Task> taskList{get;set;}  
    public List<Approved_Location__c> locList{get;set;}  
    public List<Invoice__c> invoiceList{get;set;} 
    
    public Prefundscreen(){
        prefundList = new List<Prefund__c>();
        vendorList = new List<Vendor__c>();
        taskList = new List<Task>();  
        oppList = new List<Opportunity>();
        locList = new List<Approved_Location__c>();
        invoiceList = new List<Invoice__c>();
        oppId = ApexPages.currentpage().getParameters().get('id');
        
         
        if(oppId != null)
        prefundList = [select id,Name,Prefund_Amount__c,Prefund_Date__c from Prefund__c where OppPrefund__c = : oppId ];
        
        if(oppId != null)
        oppList = [select id,Name,Contract_Manager__c,Contract_Manager_Name__c,Equipment_Description__c,Anticipated_Equipment_Delivery_Date__c,Equipment_Delivery_Timeline__c,CloseDate,Prefund_Eligible__c,UCC_1_Recorded__c,Lien_Search_Required__c,Insurance_Received__c,Lien_Search_Complete__c,Debt_Source_FF__c,Credit_Stips_Cleared__c,Schedule_Number__c,Debt_Source_lookup__c,Document_Type__c,Debt_Source__c,Expiration_Date__c,Approval_Amount__c from Opportunity where id= : oppId];
        
        if(oppId!=null)      
         locList=[select id,Name,Location_Code__c,Address1__c,City__c,State__c,Approved__c  from Approved_Location__c where Approved__c = true and Application__c = : oppId ];
                
        if(oppId!=null)      
         vendorList = [select id,Vendor_Name__c,Account_Name__c,Vendor_Prefund_Letter__c,Vendor_Account_Id__c,Prefund_Eligible__c,Regents_Approval_Status__c,Vendor_W9_Received_Date__c,Vendor_Payment_Instructions_Validated__c,Validation_Date__c from Vendor__c where Prefund_Eligible__c=true];
        
        if(oppId!=null)
          invoiceList = [select id,Vendor_Name__c,Name,Invoice_Number__c,Invoice_Total_Amount__c,InvoiceStatus__c from Invoice__c where OppInvoice__c = : oppId];
        
        for (Opportunity opp : oppList ){              
          taskList.add(new Task(whatid=opp.id,Subject='Prefund-Decision',status='Open',Task_Owner_Name1__c=opp.Contract_Manager_Name__c,ownerid=opp.Contract_Manager__c,ActivityDate=system.today()));         
        }
       
    }
    
    public void saveVendors(){
            update vendorList;            
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'Vendor Information Saved Successfully'));
        }
        
    public void createTask(){ 
                
            upsert taskList;  
            update oppList;        
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'Task created and assigned to Contract Manager successfully'));
            List<Opportunity> oppList2 = [select id,name,Owner_Email__c,Owner_Full_Name__c,Contract_Manager_Email__c,Contract_Manager__c,Contract_Manager_Name__c,Equipment_Description__c,Equipment_Delivery_Timeline__c,Anticipated_Equipment_Delivery_Date__c,CloseDate from Opportunity where id in:oppList];
       List<Messaging.SingleEmailMessage> mails = new list<Messaging.SingleEmailMessage>();     
       
       for(Opportunity oppt :oppList2)
       {
       for(Task openTask :taskList)
       {
                                 
           Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
           String[] toAddresses = new String[] {oppt.Contract_Manager_Email__c};
           String[] CcAddresses = new String[] {oppt.Owner_Email__c};
           String[] BccAddresses = new String[] {'priyas@regentscapital.com'};
           String openTaskUrl = URL.getSalesforceBaseUrl().toExternalForm()+'/'+openTask.Id;
           String oppUrl = URL.getSalesforceBaseUrl().toExternalForm()+'/'+oppt.Id;
           system.debug('---> toaddress'+toAddresses);
           string body = 'Hi ' +oppt.Contract_Manager_Name__c+ ', ';
           body += '<br><br> The following Prefund Decision open task has been assigned to you.';           
           body += '</br></br> Open task URL : ' +openTaskUrl+ ' ';             
           body += '</br></br> Opportunity URL : ' +oppUrl+ ' ';           
           body += '</br></br> Equipment Description : ' +oppt.Equipment_Description__c+ ' '; 
           body += '</br></br> Equipment Delivery Date: ' +oppt.Anticipated_Equipment_Delivery_Date__c + ' '; 
           body += '</br></br> Funding Close Date : ' +oppt.CloseDate+ ' '; 
           body += '</br></br> Equipment Delivery Timeline Notes: ' +oppt.Equipment_Delivery_Timeline__c+ ' '; 
           body += '</br></br> Rep Comments : ' +openTask.description+ ' ';        
           body += '<Br><Br> Thanks,';
           body +=  '<Br> '+oppt.Owner_Full_Name__c+ '.' ; 
           mail.setSubject('Prefund Decision Open Task');
           mail.setToAddresses(toAddresses);
           mail.setCcAddresses(CcAddresses);
           mail.setBccAddresses(BccAddresses);
           mail.setHtmlBody(body); 
           mails.add(mail);
           
        }
        }
            Messaging.sendEmail(mails);
        }
}

Test Class Code Coverage is only 58%. Highlighted code above is  not covered in test class

 
@isTest
public class PrefundscreenTest{

    public static testMethod void method1(){
        
        List<Opportunity> oppList =new List<Opportunity>();
        List<Task> taskList =new List<Task>();
        
        Account acc = new Account(name='test');
        insert acc;
        
        Contact con = new Contact(FirstName='test',LastName='test',AccountId=acc.id);
        insert con;
        
        Opportunity opp=new Opportunity(Name='opp test',AccountId=acc.id,StageName='Application Received',CloseDate=System.today(),Contract_Manager__c='005G00000089MnG',UCC_1_Recorded__c=system.today(),Lien_Search_Required__c=true,
        Insurance_Received__c=true,Lien_Search_Complete__c=system.today(),Debt_Source__c='Sumitomo Mitsui',Expiration_Date__c=system.today(),Approval_Amount__c = 120000.01);
        insert opp;
        oppList.add(opp);    
        //insert oppList;
        
        Prefund__c pf= new Prefund__c(OppPrefund__c = opp.id,Prefund_Amount__c=5000,Prefund_Date__c=system.today());
        insert pf;
        
        ApexPages.currentpage().getparameters().put('id',acc.id);
        Prefundscreen prefund= new Prefundscreen();
        prefund.saveVendors();  
        
        for(Opportunity opp1:oppList){
        task tsk = new task(whatid=opp1.id,Subject='Prefund-Decision',status='Open',ownerid=opp1.Contract_Manager__c,Task_Owner_Name1__c='Lauren Barteske',description='prefund decision',ActivityDate=system.today());      
        system.debug('--->tsk'+tsk);
        taskList.add(tsk);
        }
        
        insert taskList;
        
        List<Opportunity> oppList2 = [select id,name,Owner_Email__c,Owner_Full_Name__c,Contract_Manager_Email__c,Contract_Manager__c,Contract_Manager_Name__c,Equipment_Description__c,Equipment_Delivery_Timeline__c,Anticipated_Equipment_Delivery_Date__c,CloseDate from Opportunity where id in:oppList];
         
        prefund.createTask();
    }
    }

 
Best Answer chosen by Priya134
Maharajan CMaharajan C
Hi Priya,

Try the below test class:

@isTest
public class PrefundscreenTest{

    public static testMethod void method1(){
        
        List<Opportunity> oppList =new List<Opportunity>();
        List<Task> taskList =new List<Task>();
        
        Account acc = new Account(name='test');
        insert acc;
        
        Contact con = new Contact(FirstName='test',LastName='test',AccountId=acc.id);
        insert con;
        
        Opportunity opp=new Opportunity(Name='opp test',AccountId=acc.id,StageName='Application Received',CloseDate=System.today(),Contract_Manager__c='005G00000089MnG',UCC_1_Recorded__c=system.today(),Lien_Search_Required__c=true,
        Insurance_Received__c=true,Lien_Search_Complete__c=system.today(),Debt_Source__c='Sumitomo Mitsui',Expiration_Date__c=system.today(),Approval_Amount__c = 120000.01);
        insert opp;
        
        
        Prefund__c pf= new Prefund__c(OppPrefund__c = opp.id,Prefund_Amount__c=5000,Prefund_Date__c=system.today());
        insert pf;
        
        Approved_Location__c al= new Approved_Location__c(   --- Add your fields for Approved Location Record ---    );
        insert al;
        
        Vendor__c al= new Vendor__c(   --- Add your fields for vendor Record ---  );
        insert vr;
         
        Invoice__c iv= new Invoice__c(   --- Add your fields for Invoice Record with above Opportunity---  );
        insert iv;
        
        Test.setCurrentPage(Page.yourVFPageName);    // Replace your VF page Name instead of yourVFPageName
        ApexPages.currentPage().getParameters().put('id',opp.id);

        
        Prefundscreen prefund= new Prefundscreen();
        prefund.createTask();  
 
        prefund.saveVendors();
    }
    }

Thanks,
Maharajan.C

All Answers

Priya134Priya134
line 59 - 89 - is not covered in test class - please help
Maharajan CMaharajan C
Hi Priya,

Try the below test class:

@isTest
public class PrefundscreenTest{

    public static testMethod void method1(){
        
        List<Opportunity> oppList =new List<Opportunity>();
        List<Task> taskList =new List<Task>();
        
        Account acc = new Account(name='test');
        insert acc;
        
        Contact con = new Contact(FirstName='test',LastName='test',AccountId=acc.id);
        insert con;
        
        Opportunity opp=new Opportunity(Name='opp test',AccountId=acc.id,StageName='Application Received',CloseDate=System.today(),Contract_Manager__c='005G00000089MnG',UCC_1_Recorded__c=system.today(),Lien_Search_Required__c=true,
        Insurance_Received__c=true,Lien_Search_Complete__c=system.today(),Debt_Source__c='Sumitomo Mitsui',Expiration_Date__c=system.today(),Approval_Amount__c = 120000.01);
        insert opp;
        
        
        Prefund__c pf= new Prefund__c(OppPrefund__c = opp.id,Prefund_Amount__c=5000,Prefund_Date__c=system.today());
        insert pf;
        
        Approved_Location__c al= new Approved_Location__c(   --- Add your fields for Approved Location Record ---    );
        insert al;
        
        Vendor__c al= new Vendor__c(   --- Add your fields for vendor Record ---  );
        insert vr;
         
        Invoice__c iv= new Invoice__c(   --- Add your fields for Invoice Record with above Opportunity---  );
        insert iv;
        
        Test.setCurrentPage(Page.yourVFPageName);    // Replace your VF page Name instead of yourVFPageName
        ApexPages.currentPage().getParameters().put('id',opp.id);

        
        Prefundscreen prefund= new Prefundscreen();
        prefund.createTask();  
 
        prefund.saveVendors();
    }
    }

Thanks,
Maharajan.C
This was selected as the best answer
Priya134Priya134
Hi Maharajan,
That worked pefectly - code coverage is now 100%
Appreciate your help