You need to sign in to do that
Don't have an account?
kirankumarreddy punuru
part of code is not covering in test class
Hi ,
I am having one trigger and i have created a test class for this trigger to achieve code coverage but some part of code is not covering can some one tell the root cause why the part of code is not covering:
My trigger is:
if (trigger.isInsert)
{
oppTeamMembers = new List<Opportunity>([Select Name, Id,
(SELECT Id, UserId, TeamMemberRole
FROM OpportunityTeamMembers where EngTeamCreated__c= false) FROM Opportunity
WHERE Name IN :engagementName AND Type = 'New Business']);
}
else
{
oppTeamMembers = new List<Opportunity>([Select Name, Id,Related_Engagement__c,
(SELECT Id, UserId, TeamMemberRole
FROM OpportunityTeamMembers where EngTeamCreated__c= false) FROM Opportunity
WHERE Related_Engagement__c IN :engagementId AND Type != 'New Business']);
}
// from here the code is not covering in the test class
for(Opportunity opp : oppTeamMembers )
{
String EngId;
String EngCurrencyCode;
Date EngStartDate;
Date EngEndDate;
if(trigger.isInsert)
{
EngId = EngMap.get(opp.Name).Id;
EngCurrencyCode = EngMap.get(opp.Name).CurrencyIsoCode;
EngStartDate = EngMap.get(opp.Name).Billing_Start_Date__c;
EngEndDate = EngMap.get(opp.Name).Billing_End_Date__c;
}
else
{
EngId = EngagementMap.get(opp.Related_Engagement__c).Id;
EngCurrencyCode = EngagementMap.get(opp.Related_Engagement__c).CurrencyIsoCode;
EngStartDate = EngagementMap.get(opp.Related_Engagement__c).Billing_Start_Date__c;
EngEndDate = EngagementMap.get(opp.Related_Engagement__c).Billing_End_Date__c;
}
List<OpportunityTeamMember> oppTeam = new List<OpportunityTeamMember>(opp.OpportunityTeamMembers);
//Creatig new record for engagament team and assigning values to relative fields
for (integer i = 0; i < oppTeam.size(); i++)
{
Engagement_Team__c engTeam = new Engagement_Team__c();
engTeam.CurrencyIsoCode = EngCurrencyCode;
engTeam.Engagement__c = EngId;
engTeam.Start_Date__c = EngStartDate;
engTeam.End_Date__c = EngEndDate;
engTeam.Team_Member__c = oppTeam[i].UserId;
engTeam.Team_Role__c = oppTeam[i].TeamMemberRole;
engTeamList.add(engTeam);
oppTeam[i].EngTeamCreated__c = true;
oppTeamToBeUpdated.add(oppTeam[i]);
}
My test class is:
@isTest
public with sharing class CreateEngTeamTest{
public static testMethod void engagementTest(){
RecordDatabase.objectWrapper objWrap = new RecordDatabase.objectWrapper();
Boolean isApplicable = true;
//Insert Account
Map<String, String> accountValMap = new Map<String, String>{'Name' => 'Credit Suisse', 'CurrencyIsoCode' => 'USD', 'Industry' => 'Software',
'Type' => 'Customer', 'Region__c' => 'EMEA', 'Segment_dell__c' => 'LE', 'Client_Tier__c' => 'Growth'};
List<Account> lstAccount = RecordDatabase.CreateAccount(1, accountValMap);
insert lstAccount;
objWrap.account = [Select Id from Account LIMIT 1];
// Insert Product
Map<String, String> productValMap = new Map<String, String>{'Name' => 'Specialist Onshore (3-12 months)', 'Description' => 'DEF', 'productCode' => 'prod',
'isActive' => 'true'};
List<Product2> lstProduct = RecordDatabase.CreateProduct(1, productValMap);
system.debug('@@@:- '+lstProduct);
insert lstProduct;
Product2 prod = [Select Id from Product2 LIMIT 1];
// By default test can't access org data but using following method you can get standard pricebookId of org
Id pricebookId = Test.getStandardPricebookId();
// Insert a RecordType
objWrap.rType = [SELECT Id FROM RecordType where Name='FS LTP'AND IsActive = TRUE limit 1];
// Insert pricebookEntry
Map<String, String> pbEntryValMap = new Map<String, String>{'Product2ID' => prod.Id, 'Pricebook2ID' => pricebookId, 'UnitPrice' => '21321.1',
'isActive' => 'true'};
List<PricebookEntry> lstPricebookEntry = RecordDatabase.CreatePricebookEntry(1, pbEntryValMap);
system.debug('@@@:- '+lstPricebookEntry);
insert lstPricebookEntry;
PricebookEntry pbEntry = [Select Id from PricebookEntry LIMIT 1];
Trigger_Controller__c cs = new Trigger_Controller__c();
cs.Name = 'ShouldCalculate';
insert cs;
Engagement__c updEngagement = new Engagement__c();
updEngagement.Name = 'Tester Tesing Engage';
updEngagement.Billing_Start_Date__c = date.today();
updEngagement.Billing_end_Date__c = date.today().addDays(20);
updEngagement.Account__c = lstAccount[0].Id;
insert updEngagement;
Engagement__c e = [select Id,Name from Engagement__c LIMIT 1];
//Insert Opportunity
Map<String, String> opptyValMap = new Map<String, String>{'Name' => 'Test Opp1', 'CurrencyIsoCode' => 'USD', 'AccountId' => objWrap.account.Id, 'StageName' => 'Opportunity Identified',
'Type' => 'Net New', 'Region__c' => 'EMEA', 'Create_Engagement__c' => 'No', 'Service_Areas__c' => 'Consulting', 'Reason_Won_lost__c' => '--None--',
'Service_Class__c' => 'Pricing Ops Support', 'Probability' => '10', 'Cluster__c' => 'ABACUS', 'Pricebook2Id' => pricebookId};
List<Opportunity> lstOppty = RecordDatabase.CreateOpportunity(1, opptyValMap, objWrap);
system.debug('@@@ Oppty:- '+lstOppty);
insert lstOppty;
Opportunity oppty = [Select Id,Name,OwnerId, Create_Engagement__c, Final_Status__c, Type from Opportunity LIMIT 1];
List<RecordType> lstRecType = [Select Id, SobjectType, Name, DeveloperName, Description From RecordType where Name =: 'DMS Read Only'];
oppty.Create_Engagement__c = 'Yes';
oppty.Type = 'New Business';
oppty.Final_Status__c = 'Closed';
oppty.Go_Live_Month__c = date.today();
oppty.Related_Engagement__c = e.Id;
OpportunityHalperClass.shouldRunTrigger = false;
update oppty;
// inserting opportunity team member
OpportunityTeamMember otm = new OpportunityTeamMember();
otm.TeamMemberRole ='Sales Manager';
otm.UserId = oppty.OwnerId;
otm.OpportunityId = oppty.Id;
otm.EngTeamCreated__c = false;
insert otm;
// list of engagement names
List<String> engagementName = new List<String>();
engagementName .add(oppty.Name);
List<Opportunity> oppTeamMembers1 = new List<Opportunity>([Select Name, Id,
(SELECT Id, UserId, TeamMemberRole
FROM OpportunityTeamMembers where EngTeamCreated__c= false) FROM Opportunity
WHERE Name IN :engagementName AND Type = 'New Business']);
system.debug('### List of opportunities:-'+oppTeamMembers1[0].Name);
}
public static testMethod void engagementTest1(){
RecordDatabase.objectWrapper objWrap = new RecordDatabase.objectWrapper();
Boolean isApplicable = true;
//Insert Account
Map<String, String> accountValMap = new Map<String, String>{'Name' => 'Credit Suisse', 'CurrencyIsoCode' => 'USD', 'Industry' => 'Software',
'Type' => 'Customer', 'Region__c' => 'EMEA', 'Segment_dell__c' => 'LE', 'Client_Tier__c' => 'Growth'};
List<Account> lstAccount = RecordDatabase.CreateAccount(1, accountValMap);
insert lstAccount;
objWrap.account = [Select Id from Account LIMIT 1];
// Insert Product
Map<String, String> productValMap = new Map<String, String>{'Name' => 'Specialist Onshore (3-12 months)', 'Description' => 'DEF', 'productCode' => 'prod',
'isActive' => 'true'};
List<Product2> lstProduct = RecordDatabase.CreateProduct(1, productValMap);
system.debug('@@@:- '+lstProduct);
insert lstProduct;
Product2 prod = [Select Id from Product2 LIMIT 1];
// By default test can't access org data but using following method you can get standard pricebookId of org
Id pricebookId = Test.getStandardPricebookId();
// Insert a RecordType
objWrap.rType = [SELECT Id FROM RecordType where Name='FS LTP'AND IsActive = TRUE limit 1];
// Insert pricebookEntry
Map<String, String> pbEntryValMap = new Map<String, String>{'Product2ID' => prod.Id, 'Pricebook2ID' => pricebookId, 'UnitPrice' => '21321.1',
'isActive' => 'true'};
List<PricebookEntry> lstPricebookEntry = RecordDatabase.CreatePricebookEntry(1, pbEntryValMap);
system.debug('@@@:- '+lstPricebookEntry);
insert lstPricebookEntry;
PricebookEntry pbEntry = [Select Id from PricebookEntry LIMIT 1];
Trigger_Controller__c cs = new Trigger_Controller__c();
cs.Name = 'ShouldCalculate';
insert cs;
Engagement__c updEngagement = new Engagement__c();
updEngagement.Name = 'Tester Tesing Engage';
updEngagement.Billing_Start_Date__c = date.today();
updEngagement.CurrencyIsoCode = 'SGD';
updEngagement.Billing_end_Date__c = date.today().addDays(20);
updEngagement.Account__c = lstAccount[0].Id;
insert updEngagement;
IF( updEngagement.Billing_Start_Date__c == date.today()){
updEngagement.Billing_Start_Date__c = date.today().addDays(10);
update updEngagement;
}
Engagement__c e = [select Id,Name from Engagement__c LIMIT 1];
//Insert Opportunity
Map<String, String> opptyValMap = new Map<String, String>{'Name' => 'Test Opp', 'CurrencyIsoCode' => 'SGD', 'AccountId' => objWrap.account.Id, 'StageName' => 'Opportunity Identified',
'Type' => 'Net New', 'Region__c' => 'EMEA', 'Create_Engagement__c' => 'No', 'Service_Areas__c' => 'Consulting', 'Reason_Won_lost__c' => '--None--',
'Service_Class__c' => 'Pricing Ops Support', 'Probability' => '10', 'Cluster__c' => 'ABACUS', 'Pricebook2Id' => pricebookId};
List<Opportunity> lstOppty = RecordDatabase.CreateOpportunity(1, opptyValMap, objWrap);
system.debug('@@@ Oppty:- '+lstOppty);
insert lstOppty;
Opportunity oppty = [Select Id,OwnerId, Create_Engagement__c, Final_Status__c, Type from Opportunity LIMIT 1];
List<RecordType> lstRecType = [Select Id, SobjectType, Name, DeveloperName, Description From RecordType where Name =: 'DMS Read Only'];
oppty.Create_Engagement__c = 'Yes';
oppty.Type = 'Change';
oppty.Final_Status__c = 'Closed';
oppty.Go_Live_Month__c = date.today();
oppty.Related_Engagement__c = e.Id;
OpportunityHalperClass.shouldRunTrigger = false;
update oppty;
// inserting opportunity team member
OpportunityTeamMember otm = new OpportunityTeamMember();
otm.TeamMemberRole ='Sales Manager';
otm.UserId = oppty.OwnerId;
otm.OpportunityId = oppty.Id;
otm.EngTeamCreated__c = false;
insert otm;
// list of engagement ids
List<Id> engagementId = new List<Id>();
engagementId .add(e.Id);
List<Opportunity> oppTeamMembers = new List<Opportunity>([Select Name, Id,Related_Engagement__c,
(SELECT Id, UserId, TeamMemberRole
FROM OpportunityTeamMembers where EngTeamCreated__c= false) FROM Opportunity
WHERE Related_Engagement__c IN :engagementId AND Type != 'New Business']);
System.debug('@@@ Opportunities :-'+oppTeamMembers[0].Name);
}
}
I am having one trigger and i have created a test class for this trigger to achieve code coverage but some part of code is not covering can some one tell the root cause why the part of code is not covering:
My trigger is:
if (trigger.isInsert)
{
oppTeamMembers = new List<Opportunity>([Select Name, Id,
(SELECT Id, UserId, TeamMemberRole
FROM OpportunityTeamMembers where EngTeamCreated__c= false) FROM Opportunity
WHERE Name IN :engagementName AND Type = 'New Business']);
}
else
{
oppTeamMembers = new List<Opportunity>([Select Name, Id,Related_Engagement__c,
(SELECT Id, UserId, TeamMemberRole
FROM OpportunityTeamMembers where EngTeamCreated__c= false) FROM Opportunity
WHERE Related_Engagement__c IN :engagementId AND Type != 'New Business']);
}
// from here the code is not covering in the test class
for(Opportunity opp : oppTeamMembers )
{
String EngId;
String EngCurrencyCode;
Date EngStartDate;
Date EngEndDate;
if(trigger.isInsert)
{
EngId = EngMap.get(opp.Name).Id;
EngCurrencyCode = EngMap.get(opp.Name).CurrencyIsoCode;
EngStartDate = EngMap.get(opp.Name).Billing_Start_Date__c;
EngEndDate = EngMap.get(opp.Name).Billing_End_Date__c;
}
else
{
EngId = EngagementMap.get(opp.Related_Engagement__c).Id;
EngCurrencyCode = EngagementMap.get(opp.Related_Engagement__c).CurrencyIsoCode;
EngStartDate = EngagementMap.get(opp.Related_Engagement__c).Billing_Start_Date__c;
EngEndDate = EngagementMap.get(opp.Related_Engagement__c).Billing_End_Date__c;
}
List<OpportunityTeamMember> oppTeam = new List<OpportunityTeamMember>(opp.OpportunityTeamMembers);
//Creatig new record for engagament team and assigning values to relative fields
for (integer i = 0; i < oppTeam.size(); i++)
{
Engagement_Team__c engTeam = new Engagement_Team__c();
engTeam.CurrencyIsoCode = EngCurrencyCode;
engTeam.Engagement__c = EngId;
engTeam.Start_Date__c = EngStartDate;
engTeam.End_Date__c = EngEndDate;
engTeam.Team_Member__c = oppTeam[i].UserId;
engTeam.Team_Role__c = oppTeam[i].TeamMemberRole;
engTeamList.add(engTeam);
oppTeam[i].EngTeamCreated__c = true;
oppTeamToBeUpdated.add(oppTeam[i]);
}
My test class is:
@isTest
public with sharing class CreateEngTeamTest{
public static testMethod void engagementTest(){
RecordDatabase.objectWrapper objWrap = new RecordDatabase.objectWrapper();
Boolean isApplicable = true;
//Insert Account
Map<String, String> accountValMap = new Map<String, String>{'Name' => 'Credit Suisse', 'CurrencyIsoCode' => 'USD', 'Industry' => 'Software',
'Type' => 'Customer', 'Region__c' => 'EMEA', 'Segment_dell__c' => 'LE', 'Client_Tier__c' => 'Growth'};
List<Account> lstAccount = RecordDatabase.CreateAccount(1, accountValMap);
insert lstAccount;
objWrap.account = [Select Id from Account LIMIT 1];
// Insert Product
Map<String, String> productValMap = new Map<String, String>{'Name' => 'Specialist Onshore (3-12 months)', 'Description' => 'DEF', 'productCode' => 'prod',
'isActive' => 'true'};
List<Product2> lstProduct = RecordDatabase.CreateProduct(1, productValMap);
system.debug('@@@:- '+lstProduct);
insert lstProduct;
Product2 prod = [Select Id from Product2 LIMIT 1];
// By default test can't access org data but using following method you can get standard pricebookId of org
Id pricebookId = Test.getStandardPricebookId();
// Insert a RecordType
objWrap.rType = [SELECT Id FROM RecordType where Name='FS LTP'AND IsActive = TRUE limit 1];
// Insert pricebookEntry
Map<String, String> pbEntryValMap = new Map<String, String>{'Product2ID' => prod.Id, 'Pricebook2ID' => pricebookId, 'UnitPrice' => '21321.1',
'isActive' => 'true'};
List<PricebookEntry> lstPricebookEntry = RecordDatabase.CreatePricebookEntry(1, pbEntryValMap);
system.debug('@@@:- '+lstPricebookEntry);
insert lstPricebookEntry;
PricebookEntry pbEntry = [Select Id from PricebookEntry LIMIT 1];
Trigger_Controller__c cs = new Trigger_Controller__c();
cs.Name = 'ShouldCalculate';
insert cs;
Engagement__c updEngagement = new Engagement__c();
updEngagement.Name = 'Tester Tesing Engage';
updEngagement.Billing_Start_Date__c = date.today();
updEngagement.Billing_end_Date__c = date.today().addDays(20);
updEngagement.Account__c = lstAccount[0].Id;
insert updEngagement;
Engagement__c e = [select Id,Name from Engagement__c LIMIT 1];
//Insert Opportunity
Map<String, String> opptyValMap = new Map<String, String>{'Name' => 'Test Opp1', 'CurrencyIsoCode' => 'USD', 'AccountId' => objWrap.account.Id, 'StageName' => 'Opportunity Identified',
'Type' => 'Net New', 'Region__c' => 'EMEA', 'Create_Engagement__c' => 'No', 'Service_Areas__c' => 'Consulting', 'Reason_Won_lost__c' => '--None--',
'Service_Class__c' => 'Pricing Ops Support', 'Probability' => '10', 'Cluster__c' => 'ABACUS', 'Pricebook2Id' => pricebookId};
List<Opportunity> lstOppty = RecordDatabase.CreateOpportunity(1, opptyValMap, objWrap);
system.debug('@@@ Oppty:- '+lstOppty);
insert lstOppty;
Opportunity oppty = [Select Id,Name,OwnerId, Create_Engagement__c, Final_Status__c, Type from Opportunity LIMIT 1];
List<RecordType> lstRecType = [Select Id, SobjectType, Name, DeveloperName, Description From RecordType where Name =: 'DMS Read Only'];
oppty.Create_Engagement__c = 'Yes';
oppty.Type = 'New Business';
oppty.Final_Status__c = 'Closed';
oppty.Go_Live_Month__c = date.today();
oppty.Related_Engagement__c = e.Id;
OpportunityHalperClass.shouldRunTrigger = false;
update oppty;
// inserting opportunity team member
OpportunityTeamMember otm = new OpportunityTeamMember();
otm.TeamMemberRole ='Sales Manager';
otm.UserId = oppty.OwnerId;
otm.OpportunityId = oppty.Id;
otm.EngTeamCreated__c = false;
insert otm;
// list of engagement names
List<String> engagementName = new List<String>();
engagementName .add(oppty.Name);
List<Opportunity> oppTeamMembers1 = new List<Opportunity>([Select Name, Id,
(SELECT Id, UserId, TeamMemberRole
FROM OpportunityTeamMembers where EngTeamCreated__c= false) FROM Opportunity
WHERE Name IN :engagementName AND Type = 'New Business']);
system.debug('### List of opportunities:-'+oppTeamMembers1[0].Name);
}
public static testMethod void engagementTest1(){
RecordDatabase.objectWrapper objWrap = new RecordDatabase.objectWrapper();
Boolean isApplicable = true;
//Insert Account
Map<String, String> accountValMap = new Map<String, String>{'Name' => 'Credit Suisse', 'CurrencyIsoCode' => 'USD', 'Industry' => 'Software',
'Type' => 'Customer', 'Region__c' => 'EMEA', 'Segment_dell__c' => 'LE', 'Client_Tier__c' => 'Growth'};
List<Account> lstAccount = RecordDatabase.CreateAccount(1, accountValMap);
insert lstAccount;
objWrap.account = [Select Id from Account LIMIT 1];
// Insert Product
Map<String, String> productValMap = new Map<String, String>{'Name' => 'Specialist Onshore (3-12 months)', 'Description' => 'DEF', 'productCode' => 'prod',
'isActive' => 'true'};
List<Product2> lstProduct = RecordDatabase.CreateProduct(1, productValMap);
system.debug('@@@:- '+lstProduct);
insert lstProduct;
Product2 prod = [Select Id from Product2 LIMIT 1];
// By default test can't access org data but using following method you can get standard pricebookId of org
Id pricebookId = Test.getStandardPricebookId();
// Insert a RecordType
objWrap.rType = [SELECT Id FROM RecordType where Name='FS LTP'AND IsActive = TRUE limit 1];
// Insert pricebookEntry
Map<String, String> pbEntryValMap = new Map<String, String>{'Product2ID' => prod.Id, 'Pricebook2ID' => pricebookId, 'UnitPrice' => '21321.1',
'isActive' => 'true'};
List<PricebookEntry> lstPricebookEntry = RecordDatabase.CreatePricebookEntry(1, pbEntryValMap);
system.debug('@@@:- '+lstPricebookEntry);
insert lstPricebookEntry;
PricebookEntry pbEntry = [Select Id from PricebookEntry LIMIT 1];
Trigger_Controller__c cs = new Trigger_Controller__c();
cs.Name = 'ShouldCalculate';
insert cs;
Engagement__c updEngagement = new Engagement__c();
updEngagement.Name = 'Tester Tesing Engage';
updEngagement.Billing_Start_Date__c = date.today();
updEngagement.CurrencyIsoCode = 'SGD';
updEngagement.Billing_end_Date__c = date.today().addDays(20);
updEngagement.Account__c = lstAccount[0].Id;
insert updEngagement;
IF( updEngagement.Billing_Start_Date__c == date.today()){
updEngagement.Billing_Start_Date__c = date.today().addDays(10);
update updEngagement;
}
Engagement__c e = [select Id,Name from Engagement__c LIMIT 1];
//Insert Opportunity
Map<String, String> opptyValMap = new Map<String, String>{'Name' => 'Test Opp', 'CurrencyIsoCode' => 'SGD', 'AccountId' => objWrap.account.Id, 'StageName' => 'Opportunity Identified',
'Type' => 'Net New', 'Region__c' => 'EMEA', 'Create_Engagement__c' => 'No', 'Service_Areas__c' => 'Consulting', 'Reason_Won_lost__c' => '--None--',
'Service_Class__c' => 'Pricing Ops Support', 'Probability' => '10', 'Cluster__c' => 'ABACUS', 'Pricebook2Id' => pricebookId};
List<Opportunity> lstOppty = RecordDatabase.CreateOpportunity(1, opptyValMap, objWrap);
system.debug('@@@ Oppty:- '+lstOppty);
insert lstOppty;
Opportunity oppty = [Select Id,OwnerId, Create_Engagement__c, Final_Status__c, Type from Opportunity LIMIT 1];
List<RecordType> lstRecType = [Select Id, SobjectType, Name, DeveloperName, Description From RecordType where Name =: 'DMS Read Only'];
oppty.Create_Engagement__c = 'Yes';
oppty.Type = 'Change';
oppty.Final_Status__c = 'Closed';
oppty.Go_Live_Month__c = date.today();
oppty.Related_Engagement__c = e.Id;
OpportunityHalperClass.shouldRunTrigger = false;
update oppty;
// inserting opportunity team member
OpportunityTeamMember otm = new OpportunityTeamMember();
otm.TeamMemberRole ='Sales Manager';
otm.UserId = oppty.OwnerId;
otm.OpportunityId = oppty.Id;
otm.EngTeamCreated__c = false;
insert otm;
// list of engagement ids
List<Id> engagementId = new List<Id>();
engagementId .add(e.Id);
List<Opportunity> oppTeamMembers = new List<Opportunity>([Select Name, Id,Related_Engagement__c,
(SELECT Id, UserId, TeamMemberRole
FROM OpportunityTeamMembers where EngTeamCreated__c= false) FROM Opportunity
WHERE Related_Engagement__c IN :engagementId AND Type != 'New Business']);
System.debug('@@@ Opportunities :-'+oppTeamMembers[0].Name);
}
}
first of all your test case is too long. Please spit it to smaller methods so you could easly handle single cases. Also please use assertion in unit tests.
Please tell me what is engagementName in trigger?
What do you mean when you're tellin that code is not covered? Code does not execute, or it executes but SFDC is showing that this part is not covered?
To be sure that code executes please put some system.debug in it.
Thanks for your help,
1: engagementName is :list<String> engagementName = new list<String>();
2: engagementId is: list<Id>engagementId = new list<Id>();
3: List<Opportunity>oppTeamMembers = new List<Opportunity>([Select Name, Id,
(SELECT Id, UserId, TeamMemberRole
FROM OpportunityTeamMembers where EngTeamCreated__c= false) FROM Opportunity
WHERE Name IN :engagementName AND Type = 'New Business'])
The list is showing empty event if iam passing the values from the test class.
You probably didn't create correct data in test. Validate stetp by step using i.e. debug log if test data is correct.