You need to sign in to do that
Don't have an account?
Ravi K 47
code coverage issues for taskbeforedelete trigger
Below is the trigger
Below is the test class
For the above trigger the code coverage is below 75 % and i'm not able to cover the catch block.
Can any one suggest on this please.
trigger TaskBeforeDelete on Task (before delete) { Map<Id,Profile> profileMap=new Map<Id,Profile>([SELECT Id,Name FROM Profile WHERE Name IN ('Standard User')]); System.debug('profileMap==>'+profileMap); profile p=profileMap.get(UserInfo.getprofileID()); try { for (Task task : Trigger.old) { if(task.Activity_LFD_Type__c.equalsIgnoreCase('Test') && task.WhatId.getSObjectType() == account.sObjectType && task.WhatId != null && (p==null) ) { if(!Test.isRunningTest()){ task.addError('You are not permitted to delete this task'); } } } } Catch(Exception e){ throw new RuntimeException('****TaskBeforeDelete:'+':' +e.getMessage()); } }
Below is the test class
@istest public class TaskBeforeDelete_Test{ public static testMethod void BeforeDelete(){ Profile pId = [SELECT Id FROM Profile WHERE Name = 'Test Profile']; User testUser = new User(Email='test@email.com',Phone='555-555-5555', FirstName='Test',LastName='User', ProfileId=pId.Id,UserName=uniqueName + '@test' + orgId + '.com', Alias='tesUse',TimeZoneSidKey='America/New_York', LocaleSIdKey='en_US',EmailEncodingKey='UTF-8', LanguageLocaleKey='en_US'); system.runAs(testUser){ account acc= new account(); acc.name='test'; acc.Site='testsite'; insert acc; task task=new task(); task.Activity_LFD_Type__c='Test'; task.whatid=acc.id; insert task; delete task; } } }
For the above trigger the code coverage is below 75 % and i'm not able to cover the catch block.
Can any one suggest on this please.
One thing to know, its not mandatory to have 75% code coverage for triggers for deployments. But still if for a practice you need to cover the code, i just checked code coverage is lacking for the statement inside the Catch() and the code you written in if(!Test.isRunningTest()).
Hope that helps!
Lalit
It will clearly show you which lines of the trigger has been covered and which are not.
Why do you need the following line of code in your trigger?
In my opinion, you don't need this. Because the test data you create and delete would not stay in the system beyond the scope of the testing.
Thanks!