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