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