+ Start a Discussion
AddaAdda 

Help me in increasing the code coverage

Hi, I have written a trigger with apex on Case Topic assignment but I only managed to get the code coverage upto 80%. Please help in getting the code coverage upto 90% although the code is working fine. 
/**}

 ** Test class for TopicAssignmentTriggerHandler.cls
**/


  

@isTest
public class TopicAssignmentTriggerHandlerTest{

    public static testmethod void AssignmentTriggerHandlertest(){
        Test.startTest();
          
          List <Topic> TopicList=[Select Id,Name from Topic where (Name='Testimonial Feedback' OR Name='Author Feedback' OR Name='Course Feedback' OR Name='General Feedback' OR Name='Course Request' OR Name='Feature Request' OR Name='Testimonial' OR Name='Community Relations' OR Name='Careers' OR Name='Legal' OR Name='Remove From Newsletter' OR Name='Piracy' OR Name='PR' OR Name='Employment Verification')];          
          List<TopicAssignment> TopicAssignmentList=new List<TopicAssignment>();
          List<Case> CaseList=new List<Case>();
          
         //create test Account
          Account testAcc = new Account(
          Name = 'testAccount',
          BillingCountryCode = 'US',
          BillingState = 'California',
          BillingStreet = '6410 via Real',
          BillingPostalCode = '93013',
          BillingCity = 'Carpinteria',
          Industry = 'Construction',
          LDC_ID__c =  1234);
          insert testAcc ;
          System.Assertequals(testAcc.Name,'testAccount');
          
          //Create contact data
          Contact c1 = new Contact(AccountId = testAcc.id, FirstName = 'Test', LastName = 'Contact', Email='c-aray@lynda.com');
          c1.MailingState = 'California';
          c1.MailingCountry = 'United States';
          insert c1;
          System.Assertequals(c1.FirstName,'Test');
          System.Assertequals(c1.LastName,'Contact');
          
          //create case
          Case testcas1 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas1;
        
          TopicAssignment ta1 = new TopicAssignment(Topicid = TopicList.get(0).id, EntityId = testcas1.id);
          insert ta1;
          Case testcas2 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas2;
          TopicAssignment ta2 = new TopicAssignment(Topicid = TopicList.get(1).id, EntityId = testcas2.id);
          insert ta2;
          Case testcas3 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas3;
          TopicAssignment ta3 = new TopicAssignment(Topicid = TopicList.get(2).id, EntityId = testcas3.id);
          insert ta3;
          
          Case testcas4 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas4;
          TopicAssignment ta4 = new TopicAssignment(Topicid = TopicList.get(3).id, EntityId = testcas4.id);
          insert ta4;
          Case testcas5 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas5;
          TopicAssignment ta5 = new TopicAssignment(Topicid = TopicList.get(4).id, EntityId = testcas5.id);
          insert ta5;
         
          Case testcas6 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas6;
          TopicAssignment ta6 = new TopicAssignment(Topicid = TopicList.get(5).id, EntityId = testcas6.id);
          insert ta6;
          Case testcas7 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas7;
          TopicAssignment ta7 = new TopicAssignment(Topicid = TopicList.get(6).id, EntityId = testcas7.id);
          insert ta7;
          
          Case testcas8 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas8;
          TopicAssignment ta8 = new TopicAssignment(Topicid = TopicList.get(7).id, EntityId = testcas8.id);
          insert ta8;
          Case testcas9 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas9 ;
          TopicAssignment ta9 = new TopicAssignment(Topicid = TopicList.get(8).id, EntityId = testcas9.id);
          insert ta9;
          
          Case testcas10 = new Case(CurrencyIsoCode = 'USD',ContactId = c1.id,Description = 'test',Subject = 'test', Status = 'New',Priority='Low', Origin ='Customer Service', RecordTypeId='01270000000Msmq');
          insert testcas10 ;
          TopicAssignment ta10 = new TopicAssignment(Topicid = TopicList.get(9).id, EntityId = testcas10.id);
          insert ta10;
          
          
         Test.stopTest();
    }
}
Alex Edwin 8Alex Edwin 8
After you run the test in the Developer Console, go to the Trigger file (in developer console), click the code coverage drop list, which will show the lines covered by your test cases. This should provide a good idea on which lines are not covered and you can create more data to improve the coverage.

NOTE: Remember, Tests should not be written to boost code coverage. Here is an excellent article on best practices.

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_best_practices.htm
AddaAdda
Thanks Alex,

I went and checked which line is not covered. Now, I am checking that some of case topic is not covered and wrote more test case to cover that as well but now I am hitting this error, "Too many Email Invocations: 11" error, can you tell how to solve it in my above code.

Thanks
Abhishek
Alex Edwin 8Alex Edwin 8
I think you are hitting governor limit in sendEmail. Salesforce restricts it to 10 per execution context.

You can split your test methods. I believe for every test method governor limits are reset.

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_gov_limits.htm
https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods