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
Sebastian PageSebastian Page 

problem in test class i can't understand why is happening we got only 33 % code coverage

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)
            {
                if(userinfo.getName()=='Automated Workflow') 
                { 
                    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; 
                            } 
                            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;
                            }
                            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;
                            }   
                            if(cs.Subject==null || cs.Description==null)
                            {
                                cs.ownerId='00Gd00000027kH7'; 
                                cs.Spam_criteria__c='Blank Description / Subject';  
                                cs.Possible_Spam__c=true; 
                                cs.Identified_Keyword__c='Blank Subject or Description'; 
                            } 
                            
                        }
                    }
                }
            }
        }
    }
}
Hi All, 

We are getting only 33% code coverage . And rest of lines is showing in red color that not cover. 
my test class is here-
 
@isTest
public class SpamControllerTriggerTest {
    @isTest
    public Static void SpamTestmethod()
    { 
         Profile pf = [SELECT Id FROM Profile WHERE Name = 'System Administrator'];
        
        User tuser = new User(lastName = 'Automated Workflow',
                              email = 'AutomatedWorkflow@test.org',
                              Username = 'AutomatedWorkflow@test123.org',
                              EmailEncodingKey = 'ISO-8859-1',
                              Alias = 'AW',
                              TimeZoneSidKey = 'America/Los_Angeles',
                              LocaleSidKey = 'en_US',
                              LanguageLocaleKey = 'en_US',
                              ProfileId = pf.Id);
        insert tuser;
        
        system.runAs(tuser){
        
        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;
     
        
     List<case>lstcase=new List<case>();
       List<RecordType> listRecType = [select Id from RecordType where sObjectType = 'Case' And Name = 'MassBayCC_Ticket'];     
    
            Case cs = new Case(RecordTypeId = listRecType[0].Id,AccountId=acc.Id,ContactId=con.Id);
          cs.Spam_criteria__c='Based on Description';
        cs.Subject='tesr';
        cs.Description='Test Description1';
        cs.SuppliedEmail='shr.jdd@test.com';
        cs.Possible_Spam__c=true;
        cs.Identified_Keyword__c='s';
          lstcase.add(cs);
          Case cs1 = new Case(RecordTypeId = listRecType[0].Id,AccountId=acc.Id,ContactId=con.Id);
          cs1.Spam_criteria__c='Based on Webmail';
        cs1.Subject='tesr';
        cs1.Description='Test Description 2';
        cs1.SuppliedEmail='shr.jdd@test.com';
        cs1.Possible_Spam__c=true;
        cs1.Identified_Keyword__c='s';
          lstcase.add(cs1);
          Case cs2 = new Case(RecordTypeId = listRecType[0].Id,AccountId=acc.Id,ContactId=con.Id);
          cs2.Spam_criteria__c='Based on Subject';
        cs2.Subject='tesr';
        cs2.Description='Test Description 3';
        cs2.SuppliedEmail='shr.jdd@test.com';
        cs2.Possible_Spam__c=true;
        cs2.Identified_Keyword__c='s';
        
        lstcase.add(cs2);
          insert lstcase;
          
       
    }
}
}





 
Dushyant SonwarDushyant Sonwar
HI Sebastian ,

Could you post the screenshot of the code that is highlighted in red during test coverage? It will help others to understand your problem  , what might be causing issue in covering your class.

 
Dushyant SonwarDushyant Sonwar
You can take snapshot of developer console.
Andrew GAndrew G
Hi

I would hazard a guess that your issue is two-fold.

First:
your code does a loop on the Keyword__c object, but you are not inserting any data for that object.

Second:
Your test data does not address the various IF...ELSE IF ...ELSE IF... conditions..
if(cs.Description!=null && ( cs.Description).Contains(s))
        {
            //do stuff
        }
        else if(cs.Subject!=null && (cs.Subject).Contains(s))
        {
            //do stuff
        }
        else if((cs.SuppliedEmail).Contains(s))
        {
            //do stuff
        }
        if(cs.Subject==null || cs.Description==null)
        {
            //do stuff
        }
So you need to
1. Insert a keyword__c record
2. build test data that meets each IF statement criteria,

so something like:
String badWord = 'BadWord'; 
Keywords__c keyword = new KeyWords__c(Name = badWord);

            List<case>lstcase=new List<case>();
            List<RecordType> listRecType = [select Id from RecordType where sObjectType = 'Case' And Name = 'MassBayCC_Ticket'];

            Case cs = new Case(RecordTypeId = listRecType[0].Id,AccountId=acc.Id,ContactId=con.Id);
            cs.Subject = 'Test Case 1';
            cs.Description = badWord ;
            cs.SuppliedEmail = 'shr.jdd@test.com';
            lstcase.add(cs);
            Case cs1 = new Case(RecordTypeId = listRecType[0].Id,AccountId=acc.Id,ContactId=con.Id);
            cs1.Subject = badWord ;
            cs1.Description = 'Test Case 2';
            cs1.SuppliedEmail = 'shr.jdd@test.com';
            lstcase.add(cs1);
            Case cs2 = new Case(RecordTypeId = listRecType[0].Id,AccountId=acc.Id,ContactId=con.Id);
            cs2.Subject = 'Test Case 3';
            cs2.Description = 'Test Case 3';
            cs2.SuppliedEmail = badWord  + '@test.com';
            lstcase.add(cs1);

            Case cs3 = new Case(RecordTypeId = listRecType[0].Id,AccountId=acc.Id,ContactId=con.Id);
            //no subject
            //no description
            cs3.SuppliedEmail='shr.jdd@test.com';
            lstcase.add(cs2);
            insert lstcase;

        }
            List<Case> insertedCases = [SELECT Id, Subject, Spam_criteria__c, Possible_Spam__c, Identified_Keyword__c FROM Case WHERE Possible_spam__c = true];

            System.assertEquals(4, insertedCases.size());
            for(Case ic : insertedCases){
                if(ic.Subject == 'Test Case 1'){
                    System.assertEquals(keyword.Name, ic.Identified_Keyword__c);
                    System.assertEquals('Based on description',ic.Spam_criteria__c;
                }
            //and so on for the other for cases
            }

And to do good test code, there should be asserts.  Otherwise, how do you really know your code works?

Regards
Andrew

p.s.  the code sample above is NOT compiled
Andrew GAndrew G
As an aside, your test data should not be inserted with the values that you would expect your code to be inserting / updating, but that is what you should be testing for.  
So the bare bones Cases are inserted with enough data to trigger / fulfill the various criteria, and then do an assert to prove that the values have been updated as expected.

Regards
Andrew
Sebastian PageSebastian Page
I am attching screenshot of the code Regards, Sebastian Page Salesforce Developer [http://blackbelthelp.com/emailer/blackbelthelp-logo.jpg] 125 S Clark, 17th Floor, Chicago, Illinois - 60603 [http://blackbelthelp.com/emailer/call-sign1.jpg] (844) BLKBELT x507 [http://blackbelthelp.com/emailer/follow.jpg] [http://blackbelthelp.com/emailer/fb-logo.jpg] [http://blackbelthelp.com/emailer/linkedin-logo.jpg] [http://blackbelthelp.com/emailer/twitter.jpg] [http://blackbelthelp.com/emailer/google-logo.jpg] [http://blackbelthelp.com/emailer/YouTube.jpg] BlackBeltHelp launched Artificial Intelligence for Higher Education. To know more contact a Ninja!