+ Start a Discussion
Akash jena 3Akash jena 3 

how to cover the hightlighted area in test class

public class CapsaveFacilityLetterEXT {
    
    public static Opportunity__c currentRecord {get;set;}
    public static Id opportunityId {get;set;}
    
    public CapsaveFacilityLetterEXT(ApexPages.StandardController controller) {
        opportunityId = ApexPages.CurrentPage().getparameters().get('Id');
        currentRecord = (Opportunity__c)controller.getRecord();
        
    }

    @AuraEnabled
    public static List<OpportunityProductWrapper> getOpportunityProducts(){
        OpportunityProductWrapper category;
        List<OpportunityProductWrapper.EquipmentType> equipmentTypes;

        List<Opportunity_Product__c> opptyProducts = [SELECT Id, Equipment__r.Name, Equipment__r.Category__c, Tenure_in_months__c, Rental_Sheet__c FROM Opportunity_Product__c WHERE Opportunity__c =: opportunityId Order By Equipment__r.Category__c];
        List<OpportunityProductWrapper> categories = new List<OpportunityProductWrapper>();

        String categoryName = '';
        for(Integer i = 0; i < opptyProducts.size(); i++){
            if(categoryName != opptyProducts[i].Equipment__r.Category__c){
                if(categoryName != ''){
                    category.EquipmentType = equipmentTypes;
                    categories.add(category);
                }
                categoryName = opptyProducts[i].Equipment__r.Category__c;
                category = new OpportunityProductWrapper();
                category.Name = categoryName;
                equipmentTypes = new List<OpportunityProductWrapper.EquipmentType>();
            }
            OpportunityProductWrapper.EquipmentType equipmentType = new OpportunityProductWrapper.EquipmentType();
            equipmentType.Name = opptyProducts[i].Equipment__r.Name;
            equipmentType.Tenor = String.valueOf(opptyProducts[i].Tenure_in_months__c);

            List<OpportunityProductWrapper.PTPP> ptpps = new List<OpportunityProductWrapper.PTPP>();
            if(String.isNotBlank(opptyProducts[i].Rental_Sheet__c))
            for(String str : opptyProducts[i].Rental_Sheet__c.split(';')){
                OpportunityProductWrapper.PTPP ptpp = new OpportunityProductWrapper.PTPP();
                ptpp.Range = str.split(':')[0];
                ptpp.Rental = str.split(':')[1];
                ptpps.add(ptpp);
            }
            equipmentType.PTPP = ptpps;
            equipmentTypes.add(equipmentType);
            if(i == opptyProducts.size()-1){
                category.EquipmentType = equipmentTypes;
                categories.add(category);
            }
        }

        return categories;
    }

    Public static String getVendorNames(){
        String ven = '';
        for(Opportunity_Product__c prod : [SELECT Id,Vendor_Name__c FROM Opportunity_Product__c WHERE Opportunity__c =: opportunityId]){
            if(String.isNotBlank(ven)){
                ven = ven +',' +prod.Vendor_Name__c ;
            }else{
                ven = prod.Vendor_Name__c;
            } 
        }
        return ven;
    }

    Public static String getEquipements(){
        String eType = '';
        for(AggregateResult ar : [SELECT Equipment__r.Name eqName, Equipment__r.Category__c eqCat FROM Opportunity_Product__c WHERE Opportunity__c =: opportunityId GROUP BY Equipment__r.Name, Equipment__r.Category__c ]){
            if(eType == '') eType = (String) ar.get('eqCat');
            else eType = eType + ',' + ar.get('eqCat');
        }
        return eType;
    }

    Public static list<Opportunity_Product__c> getOpportunityLineItems(){
        return [SELECT Equipment__r.Category__c ,Equipment__r.Name, Facility_Amount__c,Deposit_Type__c,
        Facility_Type__c, Facility_Amount_Million__c, Rental_Type__c, Rental_Frequency__c, Other__c, 
        Tenure_No_of_Payments__c, Tenure_Years__c, Vendor_Name__c, Tenure_in_months__c, Processing_Fee__c, 
        Rental_Sheet__c, Security_Deposit__c,Standard_Asset__c,Useful_Life_Obtained_From__c,Decommissioning_Efforts__c,
        Nature_of_Usage_of_the_Equipment__c,Equipment_Embedded_to_the_Ground__c,Responsible_for_Maintenance__c,
        Equipment_Adequately_Insured__c,Responsible_for_the_Insurance__c,Customers_Building_Leases_Term__c,
        Warranty_Maintenance__c,Asset_Maintenance_Services__c ,PNI__c,GRV__c,XIRR__c,Recovery_Statergy__c
        FROM Opportunity_Product__c WHERE Opportunity__c =: opportunityId ];
    }

    Public static List<Contact> getContacts(){
        return [SELECT Name, Designation__c, Phone, Email FROM Contact WHERE AccountId =:  currentRecord.Account__c  ];
    }


}




& this test class I have written ,
@istest
public class CapsaveFacilityLetterEXTTest {
    @testsetup
    Public static void Caps(){
        Account acc = new Account();
        acc.Name = 'Test Account1';
           insert acc;
        
        Contact con = new Contact();
        con.Lastname = 'TestD';
        con.Designation__c = 'xyz';
        con.Phone = '12345';
        con.Email = 'abc@ghghd.com';
        con.AccountId = acc.id;
        insert con;
        
        Equipment__c eqp = new Equipment__c();
        eqp.Name = 'E1';
        eqp.Category__c = 'B';
        insert eqp;
        
        Opportunity__c opp = new Opportunity__c();
        opp.Opportunity_Name__c = 'Test A';
        opp.Stage__c = 'Prospect';
        opp.CommercialFrom__c = 'Rent Alpha Private Limited';
        opp.Account__c = acc.id;
        insert opp;
        
        Opportunity_Product__c Op = new Opportunity_Product__c();
        Op.Equipment__c = eqp.id; 
        Op.Opportunity__c = opp.id;
        Op.Tenure_in_months__c = 1234;
        insert Op;
                  
    }
    @isTest
    Private static void CapSave(){
        Test.startTest();
        List<Contact> con = [SELECT  id,Name, Designation__c, Phone, Email,AccountId FROM Contact LIMIT 1];
        //system.debug('con---->'+con);
        List<Opportunity_Product__c> Op = [SELECT id,Equipment__c,Tenure_in_months__c,Opportunity__c  from Opportunity_Product__c LIMIT 1];
        Op[0].Vendor_Name__c = 'TestC';
        update Op;
        //system.debug('Op---->'+Op);
        CapsaveFacilityLetterEXT.getOpportunityProducts();
        CapsaveFacilityLetterEXT.getVendorNames();
        CapsaveFacilityLetterEXT.getOpportunityLineItems();
        CapsaveFacilityLetterEXT.getOpportunityLineItems();
        CapsaveFacilityLetterEXT.getEquipements();
        //CapsaveFacilityLetterEXT.getContacts();  
        Test.stopTest();
    }
    @isTest
    Private static void opportunity(){
        Opportunity__c op = [Select id, Name from Opportunity__c Limit 1];
        //Opportunity__c o1 = new Opportunity__c(Name='Test 2');
        apexpages.StandardController sc = new apexpages.StandardController(op);
        CapsaveFacilityLetterEXT c = new CapsaveFacilityLetterEXT(sc);
        
    }    

}
AnudeepAnudeep (Salesforce Developers) 
I recommend checking two things

1.  Check if you are populating the opptyProducts list
2.  Ensure you meet the following critiera (Requires populating categoryName in test class)
 
if(categoryName != opptyProducts[i].Equipment__r.Category__c){