You need to sign in to do that
Don't have an account?
Sebastian Page
We are getting only 33% code coverage . And rest of lines is showing in red color that not cover.
my test class is here-
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; } } }
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.
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.. So you need to
1. Insert a keyword__c record
2. build test data that meets each IF statement criteria,
so something like:
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
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