Salesforce Apex Testing Assert Methods


Example

static TestMethod void DmlTest() {
    List<Contact> allContacts = [SELECT Id FROM Contact];
    System.assert(allContacts.isEmpty());

    Contact testContact = new Contact(FirstName = 'John', LastName = 'Doe');
    insert testContact;
    allContacts = [SELECT Id FROM Contact];
    System.assertNotEquals(0, allContacts.size(), 'Optional message in case of failure');
    
    delete allContacts;
    allContacts = [SELECT Id FROM Contact];
    System.assertEquals(0, allContacts.size());
}