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
MAITREYEE DINGARE 3MAITREYEE DINGARE 3 

Test Class for Update List APEX Class

Hi, 
I have an APEX Class that is updating a list and later I am presenting that list via LWC on the Lightning Record Page. 
I would like to write a test Class for the APEX Class. 
APEX Class:
public class PriceBookEntriesFromAccount {
    
    @AuraEnabled
    public static List<BB_Produkt__c> getPriceBookEntries(String artId) {
        List<BB_Produkt__c> bb = 
            [SELECT Name, Id, Berichsartikel__c, Berichsartikel_Copy__c, Produkt__r.Name, Verkostung__c, Kaufinteresse__c, Unternehmen__r.Name, CreatedDate 
             FROM BB_Produkt__c 
             WHERE Unternehmen__r.Id = :[SELECT Unternehmen__r.Name FROM Besuchsbericht__c WHERE Id =:artId].Unternehmen__r.Id
            ORDER BY CreatedDate DESC];
        //Add isAccessible() check
        return bb;
    }
    
    @AuraEnabled
	public static void updateRelatedList(List<BB_Produkt__c> Bblist)
    {
        if(Bblist!= null && Bblist.size()>0)
        {
            update Bblist;
            Bblist.sort();
        }
    }
}
So far this is what I have written, but I am getting 0% code coverage:
@isTest(SeeAllData=true)
public class testPriceBookEntriesFromAccount {
    
    public static testMethod void testBB_Produkt()
	{

		Account createAccount =new Account(Name='TestA');
		insert createAccount;
        
        Product2 createProduct2 =new Product2(Name='TestP');
		insert createProduct2;

		BB_Produkt__c createBB_Produkt =new BB_Produkt__c(Produkt__c=createProduct2.ID, Unternehmen__c=createAccount.Id);
		insert createBB_Produkt;

		createBB_Produkt.Feedback_Verkostung_1_5__c = 2;
		update createBB_Produkt;
	}
}


 
Best Answer chosen by MAITREYEE DINGARE 3
Maharajan CMaharajan C
Hi MAITREYEE,

Your test class needs to be like below:
 
@isTest
public class testPriceBookEntriesFromAccount {
    
    public static testMethod void testBB_Produkt()
	{

		Account createAccount =new Account(Name='TestA');
		insert createAccount;
		
		Product2 p = new Product2();
		p.Name = 'Test Product';
		p.Description='Test Product Entry For Product';
		p.productCode = 'test Prod';
		p.isActive = true;
		insert p;
		
		/*PricebookEntry standardPrice = new PricebookEntry();
		standardPrice.Pricebook2Id = Test.getStandardPricebookId();
		standardPrice.Product2Id = p.Id;
		standardPrice.UnitPrice = 100;
		standardPrice.IsActive = true;
		standardPrice.UseStandardPrice = false;
		insert standardPrice ;  */
		
		// Add if there is any other mandatory fields are there in below Besuchsbericht__c
		Besuchsbericht__c create_Besuchsbericht = new Besuchsbericht(Unternehmen__c = createAccount.Id, name = 'Test 123');
		insert create_Besuchsbericht;
		
		// Add if there is any other mandatory fields are there in below BB_Produkt__c
		BB_Produkt__c createBB_Produkt =new BB_Produkt__c(Produkt__c=p.ID, Unternehmen__c=createAccount.Id,Feedback_Verkostung_1_5__c = 2);
		insert createBB_Produkt;
		
		List<BB_Produkt__c> prodList = new List<BB_Produkt__c>{createBB_Produkt};
		
		Test.startTest();
		PriceBookEntriesFromAccount.getPriceBookEntries(create_Besuchsbericht.Id);
		PriceBookEntriesFromAccount.updateRelatedList(prodList);
		Test.stopTest();
	}
}

Please don't use the (SeeAllData=true) in test class

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi MAITREYEE,

Your test class needs to be like below:
 
@isTest
public class testPriceBookEntriesFromAccount {
    
    public static testMethod void testBB_Produkt()
	{

		Account createAccount =new Account(Name='TestA');
		insert createAccount;
		
		Product2 p = new Product2();
		p.Name = 'Test Product';
		p.Description='Test Product Entry For Product';
		p.productCode = 'test Prod';
		p.isActive = true;
		insert p;
		
		/*PricebookEntry standardPrice = new PricebookEntry();
		standardPrice.Pricebook2Id = Test.getStandardPricebookId();
		standardPrice.Product2Id = p.Id;
		standardPrice.UnitPrice = 100;
		standardPrice.IsActive = true;
		standardPrice.UseStandardPrice = false;
		insert standardPrice ;  */
		
		// Add if there is any other mandatory fields are there in below Besuchsbericht__c
		Besuchsbericht__c create_Besuchsbericht = new Besuchsbericht(Unternehmen__c = createAccount.Id, name = 'Test 123');
		insert create_Besuchsbericht;
		
		// Add if there is any other mandatory fields are there in below BB_Produkt__c
		BB_Produkt__c createBB_Produkt =new BB_Produkt__c(Produkt__c=p.ID, Unternehmen__c=createAccount.Id,Feedback_Verkostung_1_5__c = 2);
		insert createBB_Produkt;
		
		List<BB_Produkt__c> prodList = new List<BB_Produkt__c>{createBB_Produkt};
		
		Test.startTest();
		PriceBookEntriesFromAccount.getPriceBookEntries(create_Besuchsbericht.Id);
		PriceBookEntriesFromAccount.updateRelatedList(prodList);
		Test.stopTest();
	}
}

Please don't use the (SeeAllData=true) in test class

Thanks,
Maharajan.C
This was selected as the best answer
MAITREYEE DINGARE 3MAITREYEE DINGARE 3
Hello Maharajan,

Thank you for your reply. It worked! Coverage 100% 
Thanks a lot.