You need to sign in to do that
Don't have an account?
glynn
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
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.
All Answers
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?
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.
Thank you G for your help! This works for me! I'm starting to understand Unit Tests a bit more.