0

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){ 
        setIds.add(portf.Id); 
    } 

    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
        if(mapProject_Portfolio.containsKey(obj.Project_Portfolio__c)) 
//NOT COVERED       
mapProject_Portfolio.put(obj.Project_Portfolio__c, mapProject_Portfolio.get(obj.Project_Portfolio__c)+1); 
        else
        //NOT COVERED 
        mapProject_Portfolio.put(obj.Project_Portfolio__c, 1); 
    } 

        for(Project_Portfolio__c portf : newRecords){ 
            if(mapProject_Portfolio.containsKey(portf.Id)){ 
//NOT COVERED                 portf.Name.addError(System.Label.ProjectPortfolio_BusinessUnit_association); 
            } 
        } 
    } 
}

Trigger:-

trigger ProjectPortfolioTrigger on Project_Portfolio__c (before delete) {

    if(trigger.isBefore){
        if(trigger.isDelete){
            ProjectPortfolioTriggerHelper.deleteProjectPortfolios(trigger.old);
        }
    }

}

Test Class:-

@isTest
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()];

        System.runAs(user){

            Test.startTest();

            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);

            Test.stopTest();
        }
    }

    static testMethod void test_deleteProjectPortfolios(){
        setupTestData();

        ProjectPortfolioTriggerHelper.deleteProjectPortfolios(projectPortfolios2);
        System.assertEquals(3,projectPortfolios2.size());

        ProjectPortfolioTriggerHelper.deleteProjectPortfolios(projectPortfolios);
        System.assertEquals(1,projectPortfolios.size());

    }
}
0

1 Answer 1

1

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

4
  • I have created in the testdatacreator method.
    – Tinkerbell
    Commented Dec 7, 2015 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
    Commented Dec 7, 2015 at 15:32
  • it was because i've missed inserting the junction object record !
    – Tinkerbell
    Commented Dec 7, 2015 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
    Commented Dec 7, 2015 at 15:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .