You need to sign in to do that
Don't have an account?
fiona gentry
Apex class shows only 48% whereas wrote tests for all lines
Hi Folks,
Apex class shows only 48% whereas wrote tests for all lines,any idea why shows only 48%
apex code
Here is test class
Your response is appreciated
Regards
Fiona
Apex class shows only 48% whereas wrote tests for all lines,any idea why shows only 48%
apex code
public class ETRR_TaskCreationValidation { public static void isairtel account contactResearchcompleted(List<Task> ETRRTasks,Set<Id> caseIdset) { map<id, Task> mapTaskDetails ; map<Id, Case> mapCaseWithTaskList = new map<Id, Case>([select id, (Select id, Action__c from Tasks ) from Case where Id in: caseIdset]); for(Task t : ETRRTasks) { Boolean validationFlag = true; for(Task t1: mapCaseWithTaskList.get(t.WhatId).tasks) { if(t1.Action__c == 'airtel account contact Research') { validationFlag = false; } } if(validationFlag) { t.addError('Please Create a task for airtel account contact Research before creating any other Task'); } } } public static void is1stairtel contactcompleted(List<Task> ETRRTasks,Set<Id> caseIdset) { map<id, Task> mapTaskDetails ; map<Id, Case> mapCaseWithTaskList = new map<Id, Case>([select id, (Select id, Action__c from Tasks ) from Case where Id in: caseIdset]); for(Task t : ETRRTasks) { Boolean validationFlag = true; if(mapCaseWithTaskList.get(t.WhatId).tasks.size()>1){ for(Task t1: mapCaseWithTaskList.get(t.WhatId).tasks) { if( t1.Action__c == '1st airtel contact') { validationFlag = false; } } } if(validationFlag) { t.addError('Please Create a task for 1st airtel contact before creating any other Task'); } } } }
Here is test class
@isTest public class ETRR_TaskCreationValidation_Test { @isTest private static void testETRRValidation() { Id caseRecordTypeId = [select id from Recordtype where DeveloperName = 'Executive_Response' and sobjecttype = 'Case' ].Id ; Id taskrecordTypeId_ETRR = [select id, DeveloperName from RecordType where sobjecttype = 'Task' AND developername = 'ETRR_Task_Record_Type' ].Id; Case c = new case(recordtypeId = caseRecordTypeId); insETRR c; try { task t = new task(recordtypeId = taskrecordTypeId_ETRR, WhatId = c.Id ); insert t; task t2 = new task(recordtypeId = taskrecordTypeId_ETRR, WhatId = c.Id ); insert t2; } Catch(System.DmlException dmlex) { system.assETRREquals('FIELD_CUSTOM_VALIDATION_EXCEPTION', dmlex.getDmlStatusCode(0)); task t = new task(recordtypeId = taskrecordTypeId_ETRR, WhatId = c.Id, Action__c = 'airtel account airtel contact Research' ); insert t; task t1 = new task(recordtypeId = taskrecordTypeId_ETRR, WhatId = c.Id ); insert t1; task t2 = new task(recordtypeId = taskrecordTypeId_ETRR, WhatId = c.Id, Action__c = '1st airtel contact' ); insert t2; task t3 = new task(recordtypeId = taskrecordTypeId_ETRR, WhatId = c.Id ); insert t3; } } }
Your response is appreciated
Regards
Fiona
I’m assuming you’re using a trigger class with the before insert context to call the ETRR_TaskCreationValidation class. I’m also assuming you’re performing an extra SOQL query within that trigger class to get the related Case ids and storing them in the caseIdset argument.
If I understand correctly the goal here is to prevent a Task from being created if the parent Case doesn’t already have a Task with the Action__c field being ‘airtel account contact Research’.
If so, there’s a much cleaner and better way of accomplishing this but first lets go over your code. For one, you’re capturing an exception in the test class that’s not being thrown by the ETRR_TaskCreationValidation class. This is perfectly fine if you’re throwing the exception in the calling trigger class, otherwise addError wouldn’t be throwing any exceptions.
You also have a few typos such as ‘insETRR c’ and ‘system. assETRREquals’. You’re also testing your code in a one-by-one fashion, I recommend bulkifying your code. ‘mapTaskDetails’ is also not being used anywhere.
Here’s my approach to your problem, you’d preferably put this logic in the trigger handler class:
I believe this accomplishes the same without the extra SOQL call and embedded for loop. As for your 48% coverage problem, it might be coming from the lack of exception being thrown. I tested my coverage with this new approach, simply inserting a Task gives you approximately 80% coverage. Hope this answers your question.
All Answers
I’m assuming you’re using a trigger class with the before insert context to call the ETRR_TaskCreationValidation class. I’m also assuming you’re performing an extra SOQL query within that trigger class to get the related Case ids and storing them in the caseIdset argument.
If I understand correctly the goal here is to prevent a Task from being created if the parent Case doesn’t already have a Task with the Action__c field being ‘airtel account contact Research’.
If so, there’s a much cleaner and better way of accomplishing this but first lets go over your code. For one, you’re capturing an exception in the test class that’s not being thrown by the ETRR_TaskCreationValidation class. This is perfectly fine if you’re throwing the exception in the calling trigger class, otherwise addError wouldn’t be throwing any exceptions.
You also have a few typos such as ‘insETRR c’ and ‘system. assETRREquals’. You’re also testing your code in a one-by-one fashion, I recommend bulkifying your code. ‘mapTaskDetails’ is also not being used anywhere.
Here’s my approach to your problem, you’d preferably put this logic in the trigger handler class:
I believe this accomplishes the same without the extra SOQL call and embedded for loop. As for your 48% coverage problem, it might be coming from the lack of exception being thrown. I tested my coverage with this new approach, simply inserting a Task gives you approximately 80% coverage. Hope this answers your question.