You need to sign in to do that
Don't have an account?
stollmeyera
Help with System.assert - test case & trigger
I wrote a trigger to prevent specific users from deleting tasks that they do not own. It basically says that if you are part of Profile A, you will receive an error if you try to delete a task you are not assigned to. The class has 100% coverage of the trigger, but because of the nature of my trigger it throws the aforementioned error and fails the class. How would one use System.assert to tell the class that the error is good, thus passing the test?
Trigger:
trigger PreventDeleteRep on Task (Before delete) { for (Integer i = 0; i < Trigger.Old.size(); i++) { if (Trigger.Old[i].OwnerID != UserInfo.getUserId() && UserInfo.getProfileId() != '00e600000013e5e') { // Cannot delete this task Trigger.Old[i].addError('This task cannot be deleted because you are not assigned as the owner. Please contact an administrator.'); } } }
Test Class:
@isTest private class PreventDeleteTest{ static testMethod void testPrventDelete(){ //Setup the task record Task t1 = new Task( OwnerID = '00560000001N7DH', Subject = 'test', Type = 'Cold Call', Status = 'Not Started', Priority = 'Normal', WhoId = '00QP0000002OltT'); insert t1; User admin = [Select Profileid from User where Name = 'Admin' limit 1]; System.runAs(admin) { delete t1; } Task t2 = new Task( OwnerID = '00560000001N7DH', Subject = 'test', Type = 'Cold Call', Status = 'Not Started', Priority = 'Normal', WhoId = '00QP0000002OltT'); insert t2; User rep = [Select Profileid from User where Name = 'Rep' limit 1]; System.runAs(rep) { delete t2; } Task t3 = new Task( OwnerID = '00560000001N7DH', Subject = 'test', Type = 'Cold Call', Status = 'Not Started', Priority = 'Normal', WhoId = '00QP0000002OltT'); insert t3; User associate = [Select Profileid from User where Name = 'Associate' limit 1]; System.runAs(associate) { delete t3; } } }
In your test class, add a try / catch around the delete statement, and then do a system assert on the error message you get back...
Also, you need to remove those hard coded Ids - that's not going to work when you deploy to another system...create the WhoId as part of the test....
All Answers
The addError() method stops execution of code and roll backs current event(delete in your case) so that's why your trigger is failing.
Modify your trigger as below:
This would help you get rid of the error but some code coverage % will be decreased.
Regards,
Lakshman
In your test class, add a try / catch around the delete statement, and then do a system assert on the error message you get back...
Also, you need to remove those hard coded Ids - that's not going to work when you deploy to another system...create the WhoId as part of the test....
Thanks for the help everyone. I updated my trigger and test class with your suggestions as well as some other things i noticed, but the test is missing lines nine and ten which is the vital part of the trigger and is dropping the code coverage to 66%. I know it has to do with the try/catch statement, which is a new concept to me (btw I have no background in computer science). Any help is appreciated.
Trigger
test class
I'm guessing it's because you still have this line in there:
if (!Test.isRunningTest())
A such, it will never add the error
remove that if statement, and it should work fine
Yes adding of try catch will be a better option in this case as the number of lines in your trigger are less.
Remove if (!Test.isRunningTest()) from trigger and the results would be as expected, 100% code coverage..... :)
Thanks everyone, got it figured out. The main problem was that the profileID was hardcoded, which was frowned upon in an earlier post. I fixed this, removed the if statement and all is good. Thanks for the help!!
Unfortunately I have to rescind my previous post as I get the following error when I try to deply:
Failure Message: "System.AssertException: Assertion Failed", Failure Stack Trace: "Class.PreventDeleteTest2.testPrventDelete: line 63, column 9 External entry point"
This was something I was scared of as I not 100% sure how to use a try/catch statement. I am researching now but figured it wouldnt hurt to post here for archival purposes
EDIT:Forgot to mention that line 63 is where i start to assert the error message, which is this line of code:
Fixed. Changed the system assert to run off of the dml status code, which fixed the issue.
rather than referencing the actual error code: