-1

I tried this Test Class for my trigger but I always have 0% as coverage , some help please => Trigger

trigger AfterUpdate on OBJ__c (after update) {

     Id currentUser = UserInfo.getUserId();
    Id currentUserProfileId = userinfo.getProfileId();
    String profileName = [SELECT Name FROM Profile WHERE Id = :UserInfo.getProfileId()].Name;
    
        for(OBJ__c  OBJ: trigger.new) 
        {
            if ( profileName == 'MyProfile1' || profileName == 'MyProfile2'
               )
            {
            if(OBJ.Statut__c == 'Next' ) 
            {
                OBJ.Statut__c.addError(' can t modifie');
            }
    }
}
}

=> Test Class

@isTest
public with sharing class Triggers_Test {
    
      @isTest(seeAllData=true)
    public static void testAllTriggers(){
        
        Test.startTest();
         String uniqueUserName = 'standarduser' + DateTime.now().getTime() + '@testo.com';
       
         Profile p = [SELECT Id FROM Profile WHERE Name='MyProfile2'];

        User u = new User(Alias = 'standt', Email='[email protected]',
        EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
        LocaleSidKey='en_US', ProfileId = p.Id,
        TimeZoneSidKey='America/Los_Angeles',
         UserName=uniqueUserName);
        
            System.runAs(u) {
               List<OBJ__c > obj= new List<OBJ__c >(); 
                
             insert obj;
               update obj;
               delete obj;
            }
        Test.stopTest();

    }

}
5
  • Is your trigger active? Commented Nov 29, 2022 at 11:30
  • Can you confirm if your test class is running without failure? Commented Nov 29, 2022 at 11:31
  • Have you tried to see if the runAs user's details are actually being made accessible through UserInfo in your trigger?
    – Phil W
    Commented Nov 29, 2022 at 11:37
  • You were doing data validation, instead of after update trigger, you can use before update. Your requirement can done by using validation rule instead of trigger. try with this validation rule AND(ISCHANGED(Statut__c), ISPICKVAL(Statut__c,"Next"), OR($Profile.Name ='MyProfile1', $Profile.Name ='MyProfile1')) Commented Nov 29, 2022 at 12:30
  • Why are you using SeeAllData=true?
    – Adrian Larson
    Commented Nov 29, 2022 at 13:43

2 Answers 2

2

Few things to consider here:

  • You are already storing the user's profile Id in the variable currentUserProfileId, either user that in the query or don't store it in the variable at all. Currently UserInfo.getProfileId() is getting called twice which is redundant.
  • The if condition to validate the profiles can be added outside the for loop as no record data as such is required to validate the condition.
if ( profileName == 'MyProfile1' || profileName == 'MyProfile2')
    {
        for(OBJ__c  OBJ: trigger.new) 
        {
            ...
        }  
    }

Now coming to the test class:

  • First add an insert statement for the user before the custom object insertion.
  • You have just declared obj as list, but have not added any records to it, so any operations that you with that list is useless. Add some records to it and then do the operations, this should hopefully improve the coverage.
  • Also don't forget to catch the exceptions in the test class, otherwise it will fail.
1
  • 1
    the empty list is the key here
    – cropredy
    Commented Nov 29, 2022 at 18:50
1

You are trying to update an Object that has not been inserted yet and has no Salesforce Id. I am surprised you are not getting an error stating that. You need to insert your OBJ__c record first, then perform an update.

1
  • ,I just forgot to put the line of the insertion , I Insert , update and delete in the end but the coverage still always 0% Commented Nov 29, 2022 at 11:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .