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
SK SGSSK SGS 

Creating the Test Class

Hi All,
i am new to the Apex Testing,can any tell me how to write the Test Class for the Existing  Apex Class.
Apex class:
public class NewProductNoteComponentController 
{
    public class My1Exception extends Exception {}
    @AuraEnabled
    public static Event getMeetingRec(Id meetingId) 
    {
        // Perform isAccessible() checks here
        return [SELECT id, subject FROM Event WHERE Id = :meetingId];
    }
    
    @AuraEnabled
    public static String saveProductNote(CustomerProduct__c productNote, Id meetingId) 
    {
        
        //Query for the CustomerProduct__c record types
        List<RecordType> rtypes = [Select Id From RecordType where Name='MoM Product' AND SobjectType = 'CustomerProduct__c'];
        Event acId=[SELECT WhatId,MeetingName__c FROM Event WHERE Id = :meetingId];
        CustomerProduct__c newRecord = new CustomerProduct__c();
        
        system.debug('Product name in CP '+productNote.Product__c);
        system.debug('meeting Id in CP '+meetingId);
        
        List<CustomerProduct__c> checkCPRecord=[SELECT Id FROM CustomerProduct__c WHERE Product__c =:productNote.Product__c AND MeetingId__c =:meetingId];    
        
        if(checkCPRecord.size()>0)
        {
            System.debug('check condition');
            return ''; 
           
        }
        else
        {
            // Perform isAccessible() and isUpdateable() checks here
            newRecord.Product__r = productNote.Product__r;
            newRecord.Account__c=acId.WhatId;
            newRecord.MeetingName__c=acId.MeetingName__c;
            newRecord.MeetingNotes__c = productNote.MeetingNotes__c;
            newRecord.MeetingId__c = meetingId;
            newRecord.recordtypeid = rtypes.get(0).Id;
            try
            {
                
                insert newRecord;
                
             }
            catch(DmlException ex)
            {
                throw new AuraHandledException(ex.getMessage());
            }    
           return 'Record Added Successfully';  
        }
           
    }           

}
i am tried with another test class for sample apex class but it will through the error for method does not exist.can any one give any idea for this
apex class
public class Clone_Quote 
{
    
     @AuraEnabled 
    public static  Quote getQuoteDetails(string recordId)
    {
        
        return [SELECT AccountId,AgentName__c,BankDetails__c,BankName__c,BillingAddress,BillingCity,
                BillingCountry,BillingGeocodeAccuracy,BillingLatitude,BillingLongitude,BillingName,BillingPostalCode,
                BillingState,BillingStreet,CommissionType__c,ContactId,ContractId,CurrencyIsoCode,
                CustomerAddress__c,CustomerProduct__c,CustomerType__c,
                DateForTheNextCall__c,DeliveryTerms__c,Description,Email,ExpirationDate,Fax,ModeOfTransport__c,
                OpportunityId,PaymentTerms__c,Phone,PlantAddress__c,Pricebook2Id,Product__c,Quantity__c,
                QuoteOrderForm__c,SalesPrice__c,SalesType__c,ShippingAddress,ShippingCity,ShippingCountry,
                ShippingGeocodeAccuracy,ShippingHandling,ShippingLatitude,ShippingLongitude,ShippingName,
                ShippingPostalCode,ShippingState,ShippingStreet,Status,Type__c FROM Quote WHERE Id=:recordId];
       
        
    }    

}
test class
@isTest
private class Clone_Quote_TC {
     @isTest static  void myTest() {
        Clone_Quote q1 = new Clone_Quote();
        q1.getQuoteDetails();
         
         }
    }
thanks in advance.
Raj VakatiRaj Vakati
For First Class
@isTest
Private Class NewProductNoteComponentControllerTest
{

	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 ; 
		  
	 NewProductNoteComponentController.getMeetingRec(mo.Id);
           	 NewProductNoteComponentController.saveProductNote(co , mo.Id);

		
		 
          Test.StopTest();
	}
}

For Second Class 
 
@isTest
private class Clone_Quote_TC {
     @isTest static  void myTest() {
		 
		  Account objAccount = new Account(Name = 'Test Acc1', BillingCity = 'Test City', BillingState = 'Test State', 
                                                                    BillingStreet = 'Test Street', BillingPostalCode = '12345', 
                                                                    BillingCountry = 'Test Country', Phone = '123456');

        insert objAccount;
        
        
        Product2 objProduct = new Product2(Name = 'Test product1', family = 'Cafe');
                                                    
        insert objProduct;
       
        PriceBookEntry objPBE = new PriceBookEntry(UnitPrice = 300, PriceBook2Id = Test.getStandardPricebookId(),
                                                        Product2Id = objProduct.Id, IsActive = true);
                                                               
        
        insert objPBE;
        
        Opportunity objOpp = new Opportunity(Name = 'Test Opp', AccountId = objAccount.Id, StageName = 'Verbal Confirmation', CloseDate = Date.today()+1);
        insert objOpp;
        
        Quote q=  new Quote(OpportunityId = objOpp.Id, Name = 'Test Quote' + i, Pricebook2Id = objPBE.Id, Status = 'Review Pending'));
		 insert q ; 
	 
	
        Clone_Quote q1 = new Clone_Quote();
        q1.getQuoteDetails(q.Id);
         
         }
    }

 
SK SGSSK SGS
Hi Raj,thank you for your quick response,in 2nd class at line no 21 it will through bellow error

System.DmlException: Insert failed. First exception on row 0; first error: STANDARD_PRICE_NOT_DEFINED, No standard price defined for this product: []

and the test class is

@isTest(seeAllData=true)
private class Clone_Quote_TC {
     @isTest static  void myTest() {
         
     Account objAccount = new Account(Name = 'Test Acc1', BillingCity = 'Test City', BillingState = 'Test State', 
                                                                    BillingStreet = 'Test Street', BillingPostalCode = '12345', 
                                                                    BillingCountry = 'Test Country', Phone = '123456');

        insert objAccount;        
        
        Product2 objProduct = new Product2(Name = 'Test product1', family = 'MSNL');
                                                    
        insert objProduct;
         
         Pricebook2 objPB = new Pricebook2(Name='Standard Price Book',IsActive=true);
             //[SELECT Id FROM Pricebook2 WHERE Name='Standard Price Book'];
          insert objPB;
       
        PriceBookEntry objPBE = new PriceBookEntry(UnitPrice = 300, PriceBook2Id = objPB.Id,
                                                        Product2Id = objProduct.Id, IsActive = true,UseStandardPrice=true);                                 
        insert objPBE;
        
        Opportunity objOpp = new Opportunity(Name = 'Test Opp', AccountId = objAccount.Id, StageName = 'Qualification', CloseDate = Date.today()+1);
        insert objOpp;
        
        Quote q=  new Quote(OpportunityId = objOpp.Id, Name = 'Test Quote', Pricebook2Id = objPBE.Id, Status = 'Draft');
         insert q ;      
    
        //Clone_Quote q1 = new Clone_Quote();
        //q1.getQuoteDetails(q.Id);
        Clone_Quote.getQuoteDetails(q.Id);
        
         
         
         }
    }
But the Standard price fields are not there in Product.i tried with adding price to the Pricebook also but can't findout.can you help me for this.
thanks