In a unit test class how can we verify that code didn't run or a DML statement wasn't executed because a condition wasn't met?
Contrived Example (Updated):
public static void DoStuff(List<Contact> contacts)
{
if(contacts.size() == 0)
{
// exit due to condition not met
return;
}
List<Id> contactsToFilter = new List<Id>();
for(Contact c : contacts)
{
if(string.isBlank(c.FirstName))
{
contactsToFilter.add(c.Id);
}
}
List<Contacts> contactsWithoutFirstNames =
[SELECT Id, Name
FROM CONTACT
WHERE Id in : contactsToFilter];
DELETE contactsWithoutFirstNames;
}
In the code above I would like to verify that when an empty collection was sent in that we did not execute the loop or any additional code inside the DoStuff method.
Currently in my test class I'm asserting that the records passed in are unchanged but it requires a lot of test setup (inserting fake records, etc) just to assert that c.FirstName does not equal test later on. I considered adding a property to the class to keep track of records processed but that feels like a hack.