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
glynnglynn 

Unit Test Help - First time user

Hi, I created my first ever trigger, see below. However, I've really struggled the last two days trying to Unit Test it.  I've failed miserably so far. I wonder can anyone here help me?  Here is my trigger:

 

trigger UserStatusBefore on User (before update) {
    
/**********************************************************************
*                                                                        
* This code fires *ONLY* when a user updates status on homepage. If user posts     *
* URL or file along with status, then FeedItemAfter trigger fires. FeedItemAfter   *
* trigger also fires when a user posts to another user's feed.                     *
*                                                                                  *
************************************************************************************/ 
    
    for (User f : trigger.new)
    {
        Datetime thisDT = System.now();
        String itemCreatedDate = thisDT.format('yyyy-MM-dd HH:mm:ss');
        String tiBod;       
           
        for (Id theId : trigger.oldMap.keySet())
           {
                User oldUsr=trigger.oldMap.get(theId);
                User newUsr=trigger.newMap.get(theId);
                String oldStatus=oldUsr.CurrentStatus;
                String newStatus=newUsr.CurrentStatus;
                String UserId1 = f.id;
                
                if (oldStatus != newStatus && null != newStatus)
                {
                    //UserProfileFeed up = [SELECT Id, Body, CommentCount, ContentData, ContentDescription, ContentFileName, ContentSize, ContentType, CreatedDate, InsertedByID, IsDeleted, LastModifiedDate, LikeCount, LinkUrl, ParentId, Title, Type FROM UserProfileFeed WITH UserId = :UserId1 LIMIT 1];
                    User u = [select id, name, email from user where id = :f.Id];                                    
                    tiBod = 'Posted Against: User Home Page' + '\n' + 'Message: ' + f.CurrentStatus + '\n' + 'Created by: ' + u.name + ' - ' + u.email + '\n' + 'Created On: ' + itemCreatedDate;
        
                    //Compile and send email message 
                    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                    String[] toAddresses = new String[] {System.Label.ChatterArchiveExternalEmailAddress};
                    mail.setToAddresses(toAddresses);
                    mail.setSubject('New UserStatus Update');
                    mail.setSaveAsActivity(false);
                    mail.setPlainTextBody(tiBod);
                    Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
                }
                else
                {    
                    //do nothing
                } 
           }          
    }    
}

 

 

 

 

Can someone show me what the Unit Test for this trigger looks like? thanks

Best Answer chosen by Admin (Salesforce Developers) 
MiddhaMiddha

In case you need testmethod, below is the test class. Also you are using Label to save the "toaddress" values rather you should use custom settings, Labels are not the right choice. You can also add asserts in the below test code as requieed.

 

 

public class UserStatusTestClass 
{
	public testMethod static void testSQL()
	{
		Test.startTest();
		String newStatus = 'New Status at ' + System.now();
		User u = new User(Id=UserInfo.getUserId(), CurrentStatus=newStatus);
        update u;
		Test.stopTest();
	}
	
}

 

 

All Answers

MiddhaMiddha

Can you please clarify when you say Unit Test, do you mean you just want to execute and test this trigger to see if this is working OR do you want to write APEX testmethod i.e. code coverage for this trigger? 

MiddhaMiddha

In case you need testmethod, below is the test class. Also you are using Label to save the "toaddress" values rather you should use custom settings, Labels are not the right choice. You can also add asserts in the below test code as requieed.

 

 

public class UserStatusTestClass 
{
	public testMethod static void testSQL()
	{
		Test.startTest();
		String newStatus = 'New Status at ' + System.now();
		User u = new User(Id=UserInfo.getUserId(), CurrentStatus=newStatus);
        update u;
		Test.stopTest();
	}
	
}

 

 

This was selected as the best answer
glynnglynn

Thank you G for your help! This works for me! I'm starting to understand Unit Tests a bit more.