You can do something in this lines:
}catch(DMLException e){
System.debug('The following exception has occurred when inserting contacts records: ' + e.getMessage());
System.Assert(e.getMessage().contains('Required fields are missing'), 'Update did not fire');
}
If you now the error you just add the e.getMessage().contains('Part of error')
.
So to test this you need to create a method in your test class that will insert negative parameters or missing data for mandatory fields, something that will trigger the error. You can then in you Developer Console catch the error and use the message on that error to populate your assert.
Check this example from on of my test classes:
@isTest static void negtiveInsertion()
{
// Getting test data
List<Contact> contacts = Contact_TestFactory.contactsNegativeInput(200);
Database.DMLOptions dml = new Database.DMLOptions();
dml.DuplicateRuleHeader.allowSave = true;
dml.DuplicateRuleHeader.runAsCurrentUser = false;
// Inserting contacts with Want_to_heard_more__c set to true
Test.startTest();
try{
database.insert(contacts, dml);
}catch(DMLException e){
System.debug('The following exception has occurred when inserting contacts records: ' + e.getMessage());
System.Assert(e.getMessage().contains('Required fields are missing'), 'Update did not fire');
}
Test.stopTest();
// Runing asserts to control insert
System.assertEquals(200, contacts.size());
}