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

Trigger : Test Coverage results

I created a test method for a trigger. After I run this method, under Code Coverage Results, it says
DepartmentTrigger (ApexTrigger)  - 1 lines not tested, 94% covered
TestDepartmentTrigger (ApexClass) - 0 lines not tested, 100% covered.

What does the above mean. Did I write my test method correctly or not? Please let me know, quick help would be appreciated.

It states you've covered 94% of your trigger logic - one line is still not covered.  While 94% is greater than the 75% requirement - I would make sure you've tested your trigger against all the possible events - eg: bulk DML (trigger invoked by more than 1 records) - negative behavior, custom exceptions, etc.
Can you please give me an example so that I can incorporate that in my test method that I created for a triggger?
This example is very clear, thanks for sending the link. So in the below test method we have the select query. This select query can only be matched using the id field because id is unique

In my test method I was trying to query on a name field (where condition in the query) and this was returning me back two rows and was giving me an exception. So I cannot use such queries in my test method? Does it have to be only using the id field just like in the below example.

Below is the example which you had sent:
  1.   // Create a Lead with a Lead Source matching a Campaign 
  2.         Lead L1 = new Lead(); 
  3.         L1.lastname = 'Create_CampaignMember_For_New_Leads'
  4.         L1.firstname = 'Test For'
  5. = 'Company ABC'
  6.         L1.leadsource = 'Matching Campaign'
  8.         insert L1; 
  9.         String holder =; 
  11.         List <CampaignMember> cm = [select id from CampaignMember where leadid = :holder limit 1]; 
  12.         system.AssertEquals(1,cm.size()); 

I think we have a disconnect.  Name is not a unique field.  Its entirely possible to have more than one record with the same name.  With this being said you should create your test data within the context of the test - this should help you deterministically evaluate your behavior.
Ok, to be clear my test data should be different and should not have same values that are already existing in the db, is that correct or not?

Below is a line in the department trigger.
company = [select Id from Department__c where Company__c = true];

So in my test data if I say  dept.Company__c it would return more than one because there is already a row with Company__c = true.

Or is it better to delete data from database when testing a trigger using the test method.
correction in my previous reply.

dept.Company__c = true