+ Start a Discussion
System Admin 949System Admin 949 

Testclass for Apex

Hi Community,
i am facing some problem for writing the test class for below apex class,can any provide sample code for this to write the test class
apex class
public class ProductNoteListController {
    
    @AuraEnabled
  public static List<CustomerProduct__c> getProductNotes(Id meetingRecId) {
    List<CustomerProduct__c> pdtNoteList =  [SELECT Id, Product__r.Id,Product__r.Name, MeetingNotes__c,MeetingId__c,RecordType.DeveloperName
            FROM CustomerProduct__c  
            WHERE MeetingId__c =: meetingRecId
            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;
  }
    
    @AuraEnabled
    public static void updateCustProdRT(Id recordId)
    {
        try
        {
            List<RecordType> rtypes = [Select Id From RecordType where Name='Customer Product' AND SobjectType = 'CustomerProduct__c'];
            CustomerProduct__c CP = [select id from CustomerProduct__c  WHERE Id=:recordId];
            CP.RecordTypeId=rtypes.get(0).Id;
            update CP;
        }
        catch(DmlException ex)
        {
            throw new AuraHandledException(ex.getMessage());
        }    
    }   
}
thanks in advance
{!Piyush_soni__c}{!Piyush_soni__c}
hi try something like this :
 
@isTest
private class testClassSample {
  static testMethod void testMethodOne() {
      CustomerProduct__c obj = new CustomerProduct__c();
	    obj.MeetingNotes__c = 'test';
		obj.MeetingId__c = '123456789123456789';
		// *********add all requred fields here before insert record *******
		
		insert obj;
		
		ProductNoteListController.getProductNotes('123456789123456789');
		
		ProductNoteListController.updateCustProdRT(obj.id);
  }
 
}

Thank, let us know if it helps you 
Raj VakatiRaj Vakati
try this
 
@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 ; 
		  
	 ProductNoteListController.getProductNotes(mo.Id);
           
			 ProductNoteListController.updateCustProdRT(co.Id);

		 
          Test.StopTest();
	}
}