1

I'm getting exception in Test class Assertion failed.

Test Class:

 @isTest (SeeAllData = False)
public class OpportunityTriggerHandlerTest {
    
    @TestSetup
    public static void testDataCreation(){
        
        //Account creation for Opportunity
        Account acc = new Account();
        acc.name = 'TestAccount';
        insert acc;
        
        
        //Opportunity Creation
        Opportunity oppty = new Opportunity();
        oppty.Name = 'TestOppty';
        oppty.AccountId = acc.Id;
        oppty.SAP_Account__c = sapAcc.Id;
        oppty.CloseDate = Date.today();
        oppty.stageName = 'Cultivate';
        oppty.Pricebook2Id = pricebookId;
        
        insert oppty;        
        System.assertEquals(oppty.Name,'TestOppty');
        
        //Create Clone of 'TestOppty'
        Opportunity cloneOpportunity = new Opportunity();
        cloneOpportunity.Name = 'Clone-TestOppty';
        cloneOpportunity.AccountId = acc.Id;
        cloneOpportunity.SAP_Account__c = sapAcc.Id;
        cloneOpportunity.ParentOpportunityId__c = oppty.Id; 
        cloneOpportunity.Child_Cloned_Opportunity__c = true; 
        cloneOpportunity.CloseDate = Date.today();
        cloneOpportunity.stageName = 'Cultivate';
        cloneOpportunity.Pricebook2Id = pricebookId;
        
        insert cloneOpportunity;
        System.assertEquals(cloneOpportunity.Name,'Clone-TestOppty');
        
    }
    
    @isTest
    private static void deleteCloneOpportunitiesPositiveTest(){
        
        List<Opportunity> parentOpportunityList = [SELECT Id, Name FROM Opportunity WHERE Name = 'TestOppty']; 
        List<Opportunity> cloneOpportunityList = [SELECT Id, Name FROM Opportunity WHERE Name = 'Clone-TestOppty'];

        Test.startTest();
        delete parentOpportunityList; 
        OpportunityTriggerHandler.deleteCloneOpportunities(parentOpportunityList); 
        System.assertEquals(0, cloneOpportunityList.size());
        Test.stopTest();
        
    }

Apex Class:

public class OpportunityTriggerHandler {
        
    public static void deleteCloneOpportunities(List<Opportunity> oldOpportunityList){
        
        Set<id> parentOpportunityIdSet = new Set<id>();
        for(Opportunity oppObj : oldOpportunityList){
            parentOpportunityIdSet.add(oppObj.id);
            
        }
        
        List<Opportunity> childClonedOpportunities = new List<Opportunity>([select id from Opportunity where ParentOpportunityId__c in : parentOpportunityIdSet]);
        
        if(childClonedOpportunities!=null && !childClonedOpportunities.isEmpty()){
          
            delete childClonedOpportunities;            
        }
        
    }
}

Apex Class Handles if Parent Opportunity is deleted then it's related cloned opportunity is also deleted.

But Test Class is failing because of following error.

System.AssertException: Assertion Failed: Expected: 0, Actual: 1

1 Answer 1

1

Each Test method executes in it's own execution context, and all DML performed are rolled back. Therefore the changes you do in one test do not carry over to the "next test".

One suggestion if data is shared across multiple test is to use @TestSetup so you can set up your data and then do multiple tests over that data.

7
  • Thank you @Alexander Aeons Torn for your reply. I have modified my code based on your suggestion. Please find the modified test class. But I'm still getting exception.
    – Rose
    Commented Sep 5, 2022 at 11:42
  • Please , share the error code you're receiving. So I can update my answer accordingly. Which assertion are you getting the error on? Commented Sep 5, 2022 at 11:47
  • Thank you for your reply. I have shared the error and updated my question. Getting following error System.AssertException: Assertion Failed: Expected: 0, Actual: 1
    – Rose
    Commented Sep 5, 2022 at 11:49
  • Did you ensure your Trigger is getting fired after delete? And that you are calling the proper method in your handler from that trigger? Everything else seems fine to me. PD: Salesforce checks emptyness of lists before performing dml so no !childClonedOpportunities.isEmpty() is needed. Commented Sep 5, 2022 at 12:08
  • Thank you @Alexander Aeons Torn for your reply. I just checked and trigger is getting fired. When I delete the Parent Opportunity Child Cloned Opportunity is also deleted. Trigger is working fine.
    – Rose
    Commented Sep 5, 2022 at 12:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .