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
AtiqahAtiqah 

Test class for Aura

Hi, I have LWC to help users search for products in my org. I need help with apex test code
 
public with sharing class FilterProduct {
    @AuraEnabled(cacheable=true)
    public static List<Product2> getproduct(
        String Name,
        String MaterialCode,
        String productType,

        ){
            String query;
            String condition = (String.isNotBlank(Name)? 'Name LIKE \'' + '%' + Name + '%\'': '');
            condition += (String.isNotBlank(MaterialCode)? (String.isNotBlank(condition) ? +' AND ' : '') +' Material_Code__c LIKE \'' + '%' +MaterialCode +'%\'': '');
            condition += (String.isNotBlank(productType)? (String.isNotBlank(condition) ? +' AND ' : '') +' product_Type__c LIKE \'' + '%' +productType +'%\'': '');
            System.debug('condition ' + condition);

            if (String.isNotBlank(condition)) {
                query =
                    'select name, Material_Code__c, product_Type__c from product2 WHERE ' +condition +' ORDER BY name limit 1000';
            } else {
                query = 'select name, Material_Code__c, product_Type__c from product2 ORDER BY Name LIMIT 200';
            }
    
            List<Product2> records = Database.query(query);
            return records;
        }
}

 
Best Answer chosen by Atiqah
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,
Can you try the below test class.
 
@istest
public class FilterProductTest {
     static testmethod void triggerTest(){
         
         Product2 p = new Product2();
p.Name = 'Test Product';
p.Description='Test Product Entry For Product';
p.productCode = 'SFDCPanther-123';
p.isActive = true;
insert p;

// insert pricebook entry for the product
PricebookEntry standardPrice = new PricebookEntry();
standardPrice.Pricebook2Id = Test.getStandardPricebookId();
standardPrice.Product2Id = p.Id;
standardPrice.UnitPrice = 100;
standardPrice.IsActive = true;
standardPrice.UseStandardPrice = false;
insert standardPrice ;

         List<Product2> plist1= FilterProduct.getproduct(p.name,'','');
         
         system.assertEquals(1, plist1.size());
         List<Product2> plist2= FilterProduct.getproduct('','','');
         
     }

}
 

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,,

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi,
Can you try the below test class.
 
@istest
public class FilterProductTest {
     static testmethod void triggerTest(){
         
         Product2 p = new Product2();
p.Name = 'Test Product';
p.Description='Test Product Entry For Product';
p.productCode = 'SFDCPanther-123';
p.isActive = true;
insert p;

// insert pricebook entry for the product
PricebookEntry standardPrice = new PricebookEntry();
standardPrice.Pricebook2Id = Test.getStandardPricebookId();
standardPrice.Product2Id = p.Id;
standardPrice.UnitPrice = 100;
standardPrice.IsActive = true;
standardPrice.UseStandardPrice = false;
insert standardPrice ;

         List<Product2> plist1= FilterProduct.getproduct(p.name,'','');
         
         system.assertEquals(1, plist1.size());
         List<Product2> plist2= FilterProduct.getproduct('','','');
         
     }

}
 

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,,

This was selected as the best answer
AtiqahAtiqah
Hi thank you for your help.  But I received 2 errors from these 2 lines
List<Product2> plist1= FilterProduct.getproduct(p.name,'','');
 List<Product2> plist2= FilterProduct.getproduct('','','');
Method does not exist or incorrect signature: void getproduct(List<Product2>) from the type FilterProduct
Method does not exist or incorrect signature: void getproduct(NULL) from the type FilterProduct

 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Atiqah,

Your apex class has small correction. The method contains one extra , can you change it as below.
 
public static List<Product2> getproduct(String Name,String MaterialCode,String productType){

This should solve the issue.

Thanks,
​​​​​​​
AtiqahAtiqah
Hi Praveen,

You mean remove ',' after productType right? Yes, I already remove it. But I still get the errors 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Are you able to save your apex class with out any issue and also test class?

If you still face issue can we connect over gmeet and resolve it .
Thanks,