Full Trigger for Test is below. I am getting 80% coverage with my test class.

trigger PreventIncidentDelete on Incident__c (before delete) {
    String profileName=[SELECT id,Name FROM Profile WHERE Id =:UserInfo.getProfileId()].Name ;

    if(System.Trigger.IsDelete)  { 

        for (Incident__c Incd : trigger.old)  {

            if (profileName != 'System Administrator' )  { 
                Incd.addError('Insufficient privilege, please do not delete this record.');



Below is my test class.

public class testPreventIncidentDeletion {

    static testmethod void testPreventIncDel(){       

        Incident__c incd = new Incident__c();   
        incd.FKClient__c =  '005F0000003abcdeAA';
        incd.FKAccount__c = '001F000001OabcdeAR';
        incd.Office_Location__c = 'New York';
        incd.Client_Project__c = 'IT - Applications';
        incd.FKCategory__c = 'a1DF000000abcdeAK';
        incd.incidentDescription__c = 'Testing prevent Incident records deletion.';
        insert incd;

        Profile p = [SELECT Id from Profile WHERE Name ='ServiceDesk Staff'];
        User u1 = new User();
        u1.Alias = 'Test1S';
        u1.Country = 'United States';
        insert u1;

        System.runAs(u1) {


                delete incd;
                system.assert(false, 'An error was exptected');

            catch(Exception e){            

                system.assert(e.getMessage().contains('Insufficient privilege, please do not delete this record.'));    

  • 2
    We can't tell under what conditions you add that error until you give us more of the trigger context. As written this question is impossible to answer other than to tell you that you need to hit the proper execution path, which is all you ever have to do to achieve code coverage. – Adrian Larson Jul 5 '17 at 13:39
  • 3
    Additionally, you will need an assert after delete incd like system.assert(false,'An Error Was Expected') so that if it does not error as expected your test will fail – Eric Jul 5 '17 at 13:49
  • I have edited it with the full trigger code. – user34688 Jul 5 '17 at 13:59
  • I edited my test class and now is 0% coverage, please advise. Thank you. – user34688 Jul 5 '17 at 14:30
  • 1
    @Qdu - Simply adding the assert will not cause 0% coverage in a passing test method. – Eric Jul 5 '17 at 14:32

As pointed out in a comment to your post, you should add an assert statement after the delete command, to make sure that the test will fail if the exception is not thrown by the code, which I believe is happening your case thus not getting more code coverage.

You can user the System.runAs(User u) utility in your test to run with a User from a profile which is not allowed in your trigger logic. See documentation in https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_tools_runas.htm

In your test you could create a new user with a different profile (such as Standard User profile), which is not allowed to delete records in your trigger logic, and put that delete statement in the runAs context, to make sure the Exception will be thrown.

  • I modified my code (see above) and now getting 0% coverage, please advise. Thank you. – user34688 Jul 5 '17 at 14:18
  • Your test is passing? Or failing with which message? – Mauricio Oliveira Jul 5 '17 at 16:10
  • I don't know how other CRUD permissions are set in your org for those objects' access. Have in mind that you are creating the Incident__c object with the test default user (System Administrator) and then trying to delete it with the user you created, which may not have access to it. You could try to both create and delete that object inside the runAs context (leave only the profile and user part outside the runAs context, move object creation and deletion and asserts inside the context). Tell me if the test passes or fails and code coverage after that. – Mauricio Oliveira Jul 5 '17 at 16:27
  • @MauricioOliveira - thats the point of the test. OP does not want that user to be able to delete and thus it should throw an error. – Eric Jul 5 '17 at 21:15
  • I am able to test 100% now. Thank you. – user34688 Jul 5 '17 at 21:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy