function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
PranayMistryPranayMistry 

Test class for an after insert trigger

Can someone please help me write a test class for an after insert trigger. I am newbie to Salesforce and trying to figure out the promote the trigger from Sandbox to Production. Below is the trigger source code.
 
trigger SetUserExpirationDate on User (after insert) {    
    List<User> ul = new List<User>();
    for (User u : Trigger.new) {    
        User usertoUpdate = new User(Id = u.Id, Expiration_Date__c = u.CreatedDate.addYears(3));
        ul.add(usertoUpdate);            
    }
    update ul;
}

 
Best Answer chosen by PranayMistry
Alexander TsitsuraAlexander Tsitsura
hi PranayM,

Try this code
@isTest public class TestUSerTrigger {
  @isTest static void test1() {
        Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 
        User u = new User(Alias = 'standt', Email='standarduser@testorg.com', 
            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = p.Id, 
            TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg.com');

        insert u;
        
        User up = [SELECT Id, Expiration_Date__c, CreatedDate FROM USer WHERE Id = :u.Id];
        System.assertEquals(up.CreatedDate.addYears(3), up.Expiration_Date__c );
  } 
}

As a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you.

Thanks,
Alex

All Answers

Alexander TsitsuraAlexander Tsitsura
hi PranayM,

Try this code
@isTest public class TestUSerTrigger {
  @isTest static void test1() {
        Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 
        User u = new User(Alias = 'standt', Email='standarduser@testorg.com', 
            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = p.Id, 
            TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg.com');

        insert u;
        
        User up = [SELECT Id, Expiration_Date__c, CreatedDate FROM USer WHERE Id = :u.Id];
        System.assertEquals(up.CreatedDate.addYears(3), up.Expiration_Date__c );
  } 
}

As a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you.

Thanks,
Alex
This was selected as the best answer
PranayMistryPranayMistry
Code Coverage only 26%

Hi Alex,

Thanks a ton for the swift response, the unit test class worked like a charm. but unfortunately the code coverage is still 26%, even though I dont have any other trigger in my ORG or any other custom code in my ORG. Can you please help me know why this would be.

Thanks,
Pranay
Shyama B SShyama B S

Hi Pranay,
Please try the below test class and let me know if you face any issues:
@isTest
public class SetUserExpirationDateTest {
@isTest
    static void userUpdate(){
        Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 
        User u = new User(Alias = 'testtwo', Email='standarduser@testorg.com', 
            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = p.Id, 
            TimeZoneSidKey='America/Los_Angeles', UserName='test@shyamaorg.com');
       
         insert u;
        
        List<User> expDateUser=new List<User>([select Expiration_Date__c from User where UserName='test@shyamaorg.com']);
        Date expDate=date.newinstance(expDateUser[0].Expiration_Date__c.year(), 
                                      expDateUser[0].Expiration_Date__c.month(), 
                                      expDateUser[0].Expiration_Date__c.day());
        
        System.assertEquals(u. Expiration_Date__c.addYears(3),expDate);
        
    } 
}
PranayMistryPranayMistry
Hi Shyama,

I get the below error

fail

 
Shyama B SShyama B S
Sorry about that. I have corrected the code:
@isTest
public class SetUserExpirationDateTest {
@isTest
    static void userUpdate(){
        Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; 
        User u = new User(Alias = 'testtwo', Email='standarduser@testorg.com', 
            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = p.Id, 
            TimeZoneSidKey='America/Los_Angeles', UserName='testtest@shyamaorg.com');
       
         insert u;
        
        List<User> expDateUser=new List<User>([select Expiration_Date__c,CreatedDate from User where UserName='testtest@shyamaorg.com']);
        System.assertEquals(expDateUser[0].CreatedDate.addYears(3),expDateUser[0].Expiration_Date__c);
        
    } 
}
Thanks,
Shyama