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
System Admin 949System Admin 949 

Test class in Lightning

Hi,
iam tried to create the test class for this below apex class but facing some issue,can any one provide some code how to write the test class for this .
apex class:
public class CustomerProductListController
{
    @AuraEnabled
    public static List<CustomerProduct__c> getProductNotes(Id meetingRecId)
    {
        List<RecordType> rtypes = [Select Id From RecordType where Name='Customer Product' AND SobjectType = 'CustomerProduct__c'];
        List<CustomerProduct__c> pdtNoteList =  [SELECT Id, Product__r.Id,Product__r.Name, MeetingNotes__c,MeetingId__c
            FROM CustomerProduct__c  
            WHERE MeetingId__c =: meetingRecId AND RecordTypeId =: rtypes.get(0).Id
            ORDER BY createdDate DESC];
      for(CustomerProduct__c cpRec : pdtNoteList)
      {
          if(cpRec.MeetingNotes__c!=Null)
          {
               cpRec.MeetingNotes__c = cpRec.MeetingNotes__c.replaceAll('<[^>]+>',' ');
          }
           
      }
      
      System.debug('**********'+pdtNoteList);
      
      return pdtNoteList;
  }    

}

thanks.

Raj VakatiRaj Vakati
Try this


Add all required fields on object 
 
@isTest
Private Class CustomerProductListControllerTest
{

	static Testmethod void CampaignMemberEx()
	{
	
		
		Test.startTest();

		 
		 Pricebook2 pb22 = new Pricebook2(Name='Addressable');
 insert pb22;

Product2 pro2 = new Product2(Product_Line__c='DIE',Name='Addressable',Product_Code_Item_Number__c='BXCD24', isActive=true);
insert pro2;


		  List<RecordType> rtypes = [Select Id From RecordType where Name='Customer Product' AND SobjectType = 'CustomerProduct__c'];
		  
		  
		  Meeting__c mo = new Meeting__c() ;
		  mo.Name ='Test';
		  mo.MeetingNotes__c ='Test';
		  insert mo ;
		  
		  
		  CustomerProduct__c co = new CustomerProduct__c() ;
		  co.Name ='Test'; 
		  co.MeetingId__c = mo.Id ;
		  // Add other filew 
		  co.Product__c = pro2.id ; 
		 
		  co.RecordTypeId =: rtypes.get(0).Id ; 
		  // add other fields 
		  
		  insert co ; 
		  
	 CustomerProductListController.getProductNotes(mo.Id);
           
		
		 
          Test.StopTest();
	}
}

 
System Admin 949System Admin 949
thank you so much for your help @Raj vakati
System Admin 949System Admin 949
rajone issue is there,this class is executed fine but it will show 0 code coverage.whats the problem i didn't get it.assert statements are needed??
apex class is
@isTest(seeAllData=true)
private class CustomerProductListController_TC {
    @isTest static  void myTest()
    {
        try
         {
            Test.startTest();
             
             Account acc = new Account(Name='Test Account');
             insert acc;
    
             Pricebook2 pb22 = new Pricebook2(Name='Standard Price Book',IsActive=true);
             insert pb22;
    
            Product2 pro2 = new Product2(Name='Test Product',ProductCode='BXCD24', isActive=true);
            insert pro2;
    
              List<RecordType> rtypes = [Select Id From RecordType where Name='Customer Product' AND SobjectType = 'CustomerProduct__c'];
                      
              Event e = new Event() ;
              e.Subject ='Meeting';
              e.DurationInMinutes=60;
              e.ActivityDateTime=system.today();
              //e.StartDateTime=system.today();
              //e.EndDateTime=system.today()+5;
              insert e ;
              
              CustomerProduct__c co = new CustomerProduct__c() ;
              co.Account__c = acc.Id;
              co.MeetingId__c = e.Id ;
              co.Product__c = pro2.id ;
              co.RecordTypeId =rtypes.get(0).Id ;
              insert co ;
              
              CustomerProductListController.getProductNotes(e.Id);        
             
              Test.StopTest();
        }
        catch(DMLException ex)
        {
            system.assertEquals(ex.getMessage(), ex.getMessage());
        }
    }
    

}