I got the following trigger and helper class and i'm only getting test coverage of 67% and no coverage on the trigger. There are few lines not getting covered in the helper class.

Helper class:- (Comments added lines are not covered)

public with sharing class ProjectPortfolioTriggerHelper {
public static void deleteProjectPortfolios(List<Project_Portfolio__c> newRecords){

    set<Id> setIds = new set<Id>(); 
        for(Project_Portfolio__c portf : newRecords){ 

    map<Id, Integer> mapProject_Portfolio = new map<Id, Integer>(); 
    for(Business_Unit_Project_Portfolio_Lookup__c obj : [SELECT Id,Project_Portfolio__c FROM Business_Unit_Project_Portfolio_Lookup__c WHERE Project_Portfolio__c IN: setIds]) { 
        //NOT COVERED
//NOT COVERED       
mapProject_Portfolio.put(obj.Project_Portfolio__c, mapProject_Portfolio.get(obj.Project_Portfolio__c)+1); 
        //NOT COVERED 
        mapProject_Portfolio.put(obj.Project_Portfolio__c, 1); 

        for(Project_Portfolio__c portf : newRecords){ 
//NOT COVERED                 portf.Name.addError(System.Label.ProjectPortfolio_BusinessUnit_association); 


trigger ProjectPortfolioTrigger on Project_Portfolio__c (before delete) {



Test Class:-

public with sharing class ProjectPortfolioTriggerHelperTest {

    private static List<Project_Portfolio__c> projectPortfolios;
    private static List<Project_Portfolio__c> projectPortfolios2;
    private static Business_Unit_Project_Portfolio_Lookup__c projectPortfoliosLookup;
    private static List<Business_Unit__c> testBusinessUnit;

    private static void setupTestData(){

        User user = [SELECT Id FROM User WHERE Id=:UserInfo.getUserId()];



            projectPortfolios2 = new List<Project_Portfolio__c>();
            projectPortfolios2.add(new Project_Portfolio__c(Name='Portfolio 1'));
            projectPortfolios2.add(new Project_Portfolio__c(Name='Portfolio 2'));
            projectPortfolios2.add(new Project_Portfolio__c(Name='Portfolio 3'));
            insert projectPortfolios2;

            projectPortfolios = new List<Project_Portfolio__c>();
            projectPortfolios.add(new Project_Portfolio__c(Name='Portfolio 4'));
            insert projectPortfolios;

            TestDataCreator.createFullScoringScheme('Default', 3, 4, 4, 20, 1000, 60);

            testBusinessUnit = TestDataCreator.createBusinessUnits(1, 'London', TestDataCreator.scoringScheme.Id, null);
            insert testBusinessUnit;

            projectPortfoliosLookup = new Business_Unit_Project_Portfolio_Lookup__c(Business_Unit__c=testBusinessUnit[0].Id, Project_Portfolio__c=projectPortfolios[0].Id);


    static testMethod void test_deleteProjectPortfolios(){




I do not see where you are creating any Business_Unit_Project_Portfolio_Lookup__c records thus why those lines are not being covered but if your are then they do not have the proper values in the Project_Portfolio__c where they match the records you created in your setupdata

You SOQL is not returning records in the for loop thus why the inside of the for is not covered.

Mock the records appropriately and you will have coverage.

In addition, you should not be calling the class directly from your test method. To test properly you should be:

  1. Creating records
  2. executing a DML delete on the appropriate records
  3. do a query for those records
  4. assert that what should have happened did.

If you then want to call the class directly from your tests to test other scenarios you can but you should first build your test out like it actually happens from the UI

  • I have created in the testdatacreator method.
    – Tinkerbell
    Dec 7 '15 at 15:31
  • @Butterfly - then they do not have the proper values in the Project_Portfolio__c where they match the records you created in your setupdata. Think it through and it will be pretty easy to see what is going on
    – Eric
    Dec 7 '15 at 15:32
  • it was because i've missed inserting the junction object record !
    – Tinkerbell
    Dec 7 '15 at 15:52
  • @Butterfly - Great then please accept this as the answer so the question can be closed and other with a similar issue can find questions with accepted answers
    – Eric
    Dec 7 '15 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.