This Tutorial will guide you how to test exception in Apex test class

@isTest
private class TestRestrictContactByName{
    @isTest static void TestVerifyDatemethod() {
       Contact con=new Contact(firstname='Test',lastname='INVALIDNAME');
       Test.startTest();
        Database.SaveResult srList = Database.insert(con, false);
        Test.stopTest();
        // Verify
        // In this case the deletion should have been stopped by the trigger,
        // so verify that we got back an error.
       Contact con1=new Contact(firstname='Test',lastname='Valuea');
       insert con1;
       Contact con2=[select lastname from Contact where id=:con1.id];
       con2.lastname='INVALIDNAME';
       srList = Database.update(con2, false);
    }
    
  
}