function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sravanthi GSravanthi G 

how to create test class for this class

public with sharing class AfterSalesLeadEditController {
    
    //This map is used for all vehicles
    public map<String, List<Lead_Details__c>> allVehiclesMap {get;set;}
    //This map is used for all vehicles
    public List<Lead_Details__c> allVehiclesList {get;set;}
    //To fetch opportunity details
    public Opportunity opp {get;set;}
    // Constructor
    public AfterSalesLeadEditController(ApexPages.StandardController controller) {
        allVehiclesMap = new Map<String,List<Lead_Details__c>>();
        allVehiclesList = new List<Lead_Details__c>();
        opp=[SELECT id,Name ,RecordType.Name,StageName,Case__c,CreatedDate,Campaign.Name,Visited_Showroom_Date_Time__c,Market__c, Type_of_Sale__c,
             Lead_Additional_Service__c,Pickup_Date_Time__c,Order_Placed_Date__c,Quotation_Date__c,Quotation_Amount__c,Order_No__c,
             Order_Print_Date__c,Order_Confirmation_Date__c,Registration_No__c,Network_Registration_Date__c,Bought_Vehicle_Brand__c,Next_Contact_Customer_Date__c,
             Bought_Vehicle_Class__c,Bought_Vehicle_Model__c,Invoice_Number__c,Invoice_Date__c,Purchase_Time__c,Received_Date_Time__c,
             Invoice_Amount__c,Qualified_Date_Time__c,UCID__c,Desired_Contact_Date__c,Price_Range__c,Test_Drive_Date__c,Preferred_Contact_Time__c,Assigned_Service_Advisor__c,
             Accepted_Date_Time__c,Purchase_Date__c,Quotation_Print_Date__c,Quotation_No__c,Assigned_Date_Time__c,Preferred_Contact_Method__c,
             Lead_Latest_Phase__c,Contact__c,Sales_Consultant__c,Dealer_Comment__c,Customer_Preferred_Representative__c,
             Customer_Specified_Dealer__c,Lead_Type__c,Assigned_Dealer__c,CloseDate,Lead_Sub_Type__c,Lead_DataSource__c,Handover_Date__c,
             CAC_Lost_Reason__c,Lead_DataSubSource__c,Lost_Reason_text__c,Lead_Desired_Service__c,Last_Successful_Call__c,
             Lead_Source__c,Lead_Priority__c,Lost_To__c,Lost_Situation__c FROM opportunity WHERE Id =: ApexPages.currentPage().getParameters().get('id')];
                
        for(Lead_Details__c ld : [Select Id, recordTypeId, Brand__c, Car_Model__c, Class__c, Color__c, Market__c, Mileage__c, Preferred__c, Related_Lead__c, Trade_In_Vehicle__c, Trim__c, Type__c, Vehicle_Model_Type__c, Year_of_Manufacture__c From Lead_Details__c Where Related_Lead__c =: ApexPages.currentPage().getParameters().get('id')]){
            // Add vehicles separately
            String leadDetailRecordTypeName = UtilRecordType.getRecordTypeNameById('Lead_Details__c', ld.RecordTypeId);
            if(!allVehiclesMap.isEmpty() && allVehiclesMap.containsKey(leadDetailRecordTypeName)){
                List<Lead_Details__c> tempLdList = allVehiclesMap.get(leadDetailRecordTypeName);
                tempLdList.add(ld);
                allVehiclesList.add(ld);
                allVehiclesMap.put(leadDetailRecordTypeName, tempLdList);
            }
            else{
                allVehiclesList.add(ld);
                allVehiclesMap.put(leadDetailRecordTypeName, new List<Lead_Details__c>{ld});
            }
        }
    }
    // Populate vehicles on edit page
    public List<Lead_Details__c> getIntVehicles(){
        System.debug('allVehiclesMap==>' + allVehiclesMap);
        if(!allVehiclesMap.isEmpty())
            return allVehiclesMap.get('Interested Vehicle');
        return null;
    }
    public List<Lead_Details__c> getCompVehicles(){
        System.debug('allVehiclesMap==>' + allVehiclesMap);
        if(!allVehiclesMap.isEmpty())
            return allVehiclesMap.get('Competitor');
        return null;
    }
    public List<Lead_Details__c> getTradeInVehicles(){
        System.debug('allVehiclesMap==>' + allVehiclesMap);
        if(!allVehiclesMap.isEmpty())
            return allVehiclesMap.get('Trade In');
        return null;
    }
    
    public void UpdateModInfo(){
        set<id> carModelIdSet = new set<id>();
        for(Lead_Details__c leDetail : allVehiclesList){
            carModelIdSet.add(leDetail.Car_Model__c);
        }
        list<Car_Model__c> cmList = [select id, brand__C,series__c,Colour__c,Trim__c from car_model__C where id in: carModelIdSet];
        for(Lead_Details__c ld : allVehiclesList){
            for(Car_Model__c cm : cmList){
                if(ld.Car_Model__c == cm.id){
                    ld.brand__C = cm.brand__C;
                    ld.Class__c = cm.series__c;
                    ld.color__c = cm.colour__c;
                    ld.trim__C = cm.trim__c;
                }
            }
        }
    }
    
    public pageReference saveOpp(){
        Update opp;
        /*
        for(Lead_Details__c l :allVehiclesList){
            l.Related_Lead__c = opp.id;
        }
        Insert allVehiclesList;
        */
        PageReference oppPage = new ApexPages.StandardController(opp).view();
        oppPage.setRedirect(true);
        return oppPage;
    }
}
Raj VakatiRaj Vakati
Modify the below code 
 
@isTest 
public class TestAfterSalesLeadEditController 
{
 static testMethod void testMethod1() 
 {
 
  Account testAcct = new Account (Name = 'My Test Account');
     insert testAcct;

    // Creates first opportunity
    Opportunity oppt = new Opportunity(Name ='New mAWS Deal',
                            AccountID = testAcct.ID,
                            StageName = 'Customer Won',
                            Amount = 3000,
                            CloseDate = System.today());

   insert oppt;

  Lead_Details__c ld = new Lead_Details__c() ;
  ld.Name='Test';
  ld.Related_Lead__c = oppt.Id ; 
  insert ld ; 
  
  Car_Model__c cm = new Car_Model__c() ;
  cm.Name ='Test' ;
  insert cm ; 
  
  
  
  
 
 Test.startTest();
  Test.setCurrentPage(Page.YOUR_PAGE);
  ApexPages.StandardSetController stdSetController = new ApexPages.StandardSetController(oppt);
  AfterSalesLeadEditController ext = new YOUR_Extension(AfterSalesLeadEditController);
 
 ext.getIntVehicles();
 ext.getCompVehicles();
 ext.getTradeInVehicles();
 ext.UpdateModInfo();
 ext.saveOpp();
 
 
 Test.stopTest();
 }
}

 
Raj VakatiRaj Vakati
@isTest 
public class TestAfterSalesLeadEditController 
{
 static testMethod void testMethod1() 
 {
 
  Account testAcct = new Account (Name = 'My Test Account');
     insert testAcct;

    // Creates first opportunity
    Opportunity oppt = new Opportunity(Name ='New mAWS Deal',
                            AccountID = testAcct.ID,
                            StageName = 'Customer Won',
                            Amount = 3000,
                            CloseDate = System.today());

   insert oppt;

  Lead_Details__c ld = new Lead_Details__c() ;
  ld.Name='Test';
  ld.Related_Lead__c = oppt.Id ; 
  insert ld ; 
  
  Car_Model__c cm = new Car_Model__c() ;
  cm.Name ='Test' ;
  insert cm ; 
  
  
  
  
 
 Test.startTest();
  Test.setCurrentPage(Page.YOUR_PAGE);
  ApexPages.StandardSetController sc = new ApexPages.StandardSetController(oppt);
  AfterSalesLeadEditController ext = new AfterSalesLeadEditController(sc);
 
 ext.getIntVehicles();
 ext.getCompVehicles();
 ext.getTradeInVehicles();
 ext.UpdateModInfo();
 ext.saveOpp();
 
 
 Test.stopTest();
 }
}