function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
devloper sfdcdevloper sfdc 

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

User-added image

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;
          
       }
    }

​​​​​​​


 
Andrew GAndrew G
by looking at the code, the issue is that you are testing explicitly for the ID of the Automated User.  When you run the test class, it runs in context of the user invoking the test class.  Since that is a user, it is not your Automated user, therefore the condition is not met.

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
 
devloper sfdcdevloper sfdc
Hi 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 
/*       Name          : SpamControllerTrigger 
        Author         : Sebastian Page
        Date           : 24th May 2019
        Description    : This trigger move tickets into Spam queue by mapping keyword  fro keyword object */



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<user> getuser=[select id from user where username='sf_XXX@XXX.org.newapi' limit 1];
    String usrId=getuser[0].id;
    List<group>queueId=[select id from group  where type='queue' and name='zBlackBeltHelp Spam' limit 1];
    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)
    {
        if(userinfo.getUserId()==usrId) {
            if(lstBasedOnDesc.size()>0)
            { 
                for(String s:lstBasedOnDesc){
                    if(cs.Description!=null && ( cs.Description).Contains(s))
                    { 
                        cs.ownerId=queueId[0].id;
                        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=queueId[0].id;
                        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=queueId[0].id;
                     cs.Spam_criteria__c='Based on Webmail';
                     cs.Possible_Spam__c=true;cs.Identified_Keyword__c=s;
                      CalculatingScoreValue.getScoreValue(s);	
                    }                                                                                                                    }}}}}}}

Test Class is
 
@isTest(seeAllData=true)
public class SpamControllerTriggerTest {
    @isTest
    public Static void SpamTestmethod()
    {
        
	   Trigger_Control__c tc=new Trigger_Control__c();
       tc.Enable_Spam_Controller_Trigger__c=true;
        tc.Name='test tc';
        insert tc;
        
        Account acc = new Account(Name='MassBay');
        insert acc;
        
        Contact con = new Contact(AccountId=acc.Id,LastName='test',Email='desd.red@test.tst');
        insert con;
        
       
        
        
        Test.startTest();
        
         
       Profile profile1 = [Select Id from Profile where name = 'System Administrator'];
       System.debug('What is the profile id ' + profile1);
       User u = new User(
            ProfileId = profile1.Id,
            Username = 'test@bbh.com',
            Alias = 'batman',
            Email='test@bbh.com',
            EmailEncodingKey='UTF-8',
            Firstname='Bruce',
            Lastname='Wayne',
            LanguageLocaleKey='en_US',
            LocaleSidKey='en_US',
            TimeZoneSidKey='America/Chicago');
            insert u;
            System.debug ('Here is the user id ' + u.id);
        
        User u1 = [SELECT id from User WHERE username = 'test@bbh.com' LIMIT 1];
        system.debug('MC Users ='+ ' ' + u1);
        System.runas(u1) {
            
            List<keywords__c>lstKeys=new List<keywords__c>();
        
        keywords__c keys01=new keywords__c();
        keys01.name='microsoft.com';
        keys01.Spam_classification__c='Based on Web-Mail';
        keys01.Act_as_Validation__c=false;
        keys01.Validation_Score__c=5;
        lstKeys.add(keys01);       
        
        keywords__c keys=new keywords__c();
        keys.name='Test Ticket';
        keys.Spam_classification__c='Based on Subject';
        keys.Act_as_Validation__c=true;
        keys.Validation_Score__c=5;
        lstKeys.add(keys); 
        
        
        keywords__c keys1=new keywords__c();
        keys1.name='microsoft.com';
        keys1.Spam_classification__c='Based on Description';
        keys1.Act_as_Validation__c=false;
        keys1.Validation_Score__c=5;
         lstKeys.add(keys1); 
            insert lstKeys;
                
        List<case>lstcase=new List<case>();
            
          
       List<RecordType> listRecType = [select Id from RecordType where sObjectType = 'Case' And Name = 'CCBC_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='test';
        cs.SuppliedEmail='xxx@test.com';
        cs.Possible_Spam__c=true;
        cs.Identified_Keyword__c='s';
          lstcase.add(cs);
           insert lstcase;
            
          Test.stopTest();
       }
}
}

Screenshot line User-added image