You need to sign in to do that
Don't have an account?

Code Coverage problem on my apex trigger
Hello All,
I am controlling our spam ticket using this trigger. And also Trigger running as expected my requirment. but I am not able to getting full code coverge by test class. Please check my test class and tell me what the actual error in my code .
Screen shot

Thanks & Regards
My Apex Trigger is
and my Test class is
I am controlling our spam ticket using this trigger. And also Trigger running as expected my requirment. but I am not able to getting full code coverge by test class. Please check my test class and tell me what the actual error in my code .
Screen shot
Thanks & Regards
My Apex Trigger is
trigger SpamControllerTrigger on Case (before insert) { /*===================================================hhjjf====================================*/ List<String> lstBasedOnDesc=new List<String>(); for(Keywords__c ks:[Select name from Keywords__c]){ lstBasedOnDesc.add(ks.name);} List<String>LstSpamkeyword=new List<String>(); /*===================================================hhjjf====================================*/ for(Trigger_Control__c tc:Trigger_Control__c.getAll().values()) { if( tc.Enable_Spam_Controller_Trigger__c==true) { for(case cs:Trigger.new) {//0050W0000061RWD sebastian //005d000000187CwAAI automated if(userinfo.getUserId()=='005d000000187CwAAI') { if(lstBasedOnDesc.size()>0) { for(String s:lstBasedOnDesc){ if(cs.Description!=null && ( cs.Description).Contains(s)) { cs.ownerId='00Gd00000027kH7'; cs.Spam_criteria__c='Based on Description'; cs.Possible_Spam__c=true;cs.Identified_Keyword__c=s; CalculatingScoreValue.getScoreValue(s); } else if(cs.Subject!=null && (cs.Subject).Contains(s)) { cs.ownerId='00Gd00000027kH7'; cs.Spam_criteria__c='Based on Subject'; cs.Possible_Spam__c=true; cs.Identified_Keyword__c=s; CalculatingScoreValue.getScoreValue(s); } else if((cs.SuppliedEmail).Contains(s)) {cs.ownerId='00Gd00000027kH7'; cs.Spam_criteria__c='Based on Webmail'; cs.Possible_Spam__c=true;cs.Identified_Keyword__c=s; CalculatingScoreValue.getScoreValue(s); } }}}}}}}
and my Test class is
@isTest public class SpamControllerTriggerTest { @isTest public Static void SpamTestmethod() { Account acc = new Account(Name='MassBay'); insert acc; Contact con = new Contact(AccountId=acc.Id,LastName='test',Email='desd.red@test.tst'); insert con; Trigger_Control__c tc=new Trigger_Control__c(); tc.Enable_Spam_Controller_Trigger__c=true; tc.Name='test tc'; insert tc; Keywords__c key=new keywords__c(name='badword'); insert key; List<case>lstcase=new List<case>(); List<RecordType> listRecType = [select Id from RecordType where sObjectType = 'Case' And Name = 'MassBay_Ticket']; Case cs = new Case(RecordTypeId = listRecType[0].Id,AccountId=acc.Id,ContactId=con.Id); cs.Spam_criteria__c='Based on Description'; cs.Subject='Testing for spam'; cs.Description=key.Name; cs.SuppliedEmail='xxx@test.com'; cs.Possible_Spam__c=true; cs.Identified_Keyword__c='s'; lstcase.add(cs); insert lstcase; } }
You will need to look at setting up a System.runas(User) for the test class.
And secondly, the use of explicit Ids in your Apex is not best practice. Look at extracting the ID using a SOQL against data with some other criteria to identify the User and / or Queue/Group.
regards
Andrew
Thanks for valueable comment , I modified our test code according to you and I am not using explisit user id in the trigger. but code coverege not incresing. After modifying , I am sharing you my Apex trigger and test class. So Please check where is fault .
apex trigger
Test Class is
Screenshot line