You need to sign in to do that
Don't have an account?
Test Coverage (Lines after if condition are not covered)
Hi friends,
The lines after If condition if(numberDaysDue<=60) (NOTE: Condition is Meeting) in my APEX code are not getting covered (These lines r shown in red color) . Any workaround Plz.
My Test Class
@isTest (seeAllData=true)
public class RenewalTest{
public static String CRON_EXP = '0 0 0 15 3 ? 2022';
RenewalOpportunity r= new RenewalOpportunity();
static testmethod void RenewalTest() {
// Test.startTest();
String jobId = System.schedule('Chidanand',CRON_EXP,new RenewalOpportunity ());
CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered,
NextFireTime
FROM CronTrigger WHERE id = :jobId];
System.assertEquals(CRON_EXP,
ct.CronExpression);
System.assertEquals(0, ct.TimesTriggered);
System.assertEquals('2022-03-15 00:00:00',
String.valueOf(ct.NextFireTime));
Date d = Date.Today();
Date dueDate = date.newInstance(2015, 3, 20);
// CSM__c csm= new CSM__c(Name='Chidanand',Customer_Success_Manager__c='Chidanand',Email='Chidanand@knstek.com');
// insert csm;
Campaign c = new Campaign(Name='Chidanand');
insert c;
Product2 prod = new Product2(Name = 'Laptop X200',
Family = 'Hardware');
insert prod;
Account a = new Account(Name='KNS',Service_End_Date__c=dueDate,Customer_Success_Manager__c='Jeff Tucker',ARR__c=500);
insert a;
Opportunity o = new Opportunity(Name='abc',AccountId=a.Id,CampaignId=c.Id,
StageName='Legal',Amount=5000,CloseDate=d,NextStep='Open',AutoCreatedFlag__c=false);
for(Account acc:[select Id,Name,Service_End_Date__c,Customer_Success_Manager__c,ARR__c
from Account acc
where
(Id NOT IN (select AccountID from opportunity
where AutoCreatedFlag__c = true ))]){
if(a.Service_End_Date__c!=NULL){
Integer numberDaysDue= d.daysBetween(a.Service_End_Date__c);
system.debug('Chidanand'+numberDaysDue);
if(numberDaysDue<=60) {
Opportunity Op= new Opportunity();
op.Name=a.Name;
op.StageName='Legal';
op.Amount=a.ARR__c;
op.AccountId=a.ID;
op.CloseDate=Date.today();
op.Customer_Success_Manager__c=a.Customer_Success_Manager__c;
op.NextStep='Won';
// op.OwnerID='005d0000001Kt9D';
op.AutoCreatedFlag__c = true;
insert op;
System.debug('Hi Chidanand');
// Id pricebookId = Test.getStandardPricebookId();
List<PriceBookEntry> entryList = new List<PriceBookEntry>();
Pricebook2 stdpb = [SELECT Id FROM Pricebook2 WHERE IsStandard = true]; // Select Price Book
PricebookEntry standardPrice = new PricebookEntry(
Pricebook2Id = stdpb .Id, Product2Id = prod.Id,
UnitPrice = 10, IsActive = true);
// entryList.add(standardPrice );
Integer counter = [select count() FROM PriceBookEntry where Product2.Id=:prod.Id And
PriceBook2.isStandard=true AND UnitPrice=10];
entryList.add(standardPrice );
if(counter==0){
insert entryList;
}
// insert entryList;
String z=prod.Id;
List<PriceBookEntry> priceBookList = [select Id, Product2Id, Product2.Id, Product2.Name FROM PriceBookEntry
where (Product2.Id=:z) And (PriceBook2.isStandard=true) LIMIT 1 ]; // Get the Price Book entry ID that we entered in the previous step
OpportunityLineItem oli = new OpportunityLineItem(); // Create new Opportunity Product
oli.OpportunityId = op.Id;
oli.PricebookEntryId=priceBookList[0].Id; // PricebookentryID of the existing pricebook entry for the specific product
oli.UnitPrice =10; // Sales Price (or List Price)
oli.Quantity=1;
insert oli;
}
}
}
}
}
APEX CODE
global class RenewalOpportunity implements Schedulable {
global void execute(SchedulableContext SC) {
RenewalOpp R= new RenewalOpp();
}
public class RenewalOpp{
RenewalOpp(){
Date d = Date.Today();
for(Account a:[select id,Name,Service_End_Date__c,Customer_Success_Manager__c,ARR__c
from Account acc
where
(Id NOT IN (select AccountID from opportunity
where AutoCreatedFlag__c = true ))])
{
if(a.Service_End_Date__c!=NULL){
// Difference between Service End date and Current date
Integer numberDaysDue= d.daysBetween(a.Service_End_Date__c);
if(numberDaysDue<=60) {
// Creating a new opportunity
Opportunity O= new Opportunity();
o.Name=a.Name;
o.StageName='Legal';
o.Amount=a.ARR__c;
o.AccountId=a.ID;
o.CloseDate=a.Service_End_Date__c;
o.Customer_Success_Manager__c=a.Customer_Success_Manager__c;
o.NextStep='Won';
o.OwnerID='005d0000001Kt9D';
o.AutoCreatedFlag__c = true;
insert o;
List<PriceBookEntry> priceBookList = [select Id, Product2Id, Product2.Id, Product2.Name FROM PriceBookEntry where Product2.Id='01td0000003SDB8' And
PriceBook2.isStandard=true LIMIT 1 ]; // Get the Price Book entry ID that we entered in the previous step
OpportunityLineItem oli = new OpportunityLineItem(); // Create new Opportunity Product
oli.OpportunityId = o.Id;
oli.PricebookEntryId=priceBookList[0].Id; // PricebookentryID of the existing pricebook entry for the specific product
oli.UnitPrice =a.ARR__c; // Sales Price (or List Price)
oli.Quantity=1;
insert oli;
}
}
}
}
}
}
The lines after If condition if(numberDaysDue<=60) (NOTE: Condition is Meeting) in my APEX code are not getting covered (These lines r shown in red color) . Any workaround Plz.
My Test Class
@isTest (seeAllData=true)
public class RenewalTest{
public static String CRON_EXP = '0 0 0 15 3 ? 2022';
RenewalOpportunity r= new RenewalOpportunity();
static testmethod void RenewalTest() {
// Test.startTest();
String jobId = System.schedule('Chidanand',CRON_EXP,new RenewalOpportunity ());
CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered,
NextFireTime
FROM CronTrigger WHERE id = :jobId];
System.assertEquals(CRON_EXP,
ct.CronExpression);
System.assertEquals(0, ct.TimesTriggered);
System.assertEquals('2022-03-15 00:00:00',
String.valueOf(ct.NextFireTime));
Date d = Date.Today();
Date dueDate = date.newInstance(2015, 3, 20);
// CSM__c csm= new CSM__c(Name='Chidanand',Customer_Success_Manager__c='Chidanand',Email='Chidanand@knstek.com');
// insert csm;
Campaign c = new Campaign(Name='Chidanand');
insert c;
Product2 prod = new Product2(Name = 'Laptop X200',
Family = 'Hardware');
insert prod;
Account a = new Account(Name='KNS',Service_End_Date__c=dueDate,Customer_Success_Manager__c='Jeff Tucker',ARR__c=500);
insert a;
Opportunity o = new Opportunity(Name='abc',AccountId=a.Id,CampaignId=c.Id,
StageName='Legal',Amount=5000,CloseDate=d,NextStep='Open',AutoCreatedFlag__c=false);
for(Account acc:[select Id,Name,Service_End_Date__c,Customer_Success_Manager__c,ARR__c
from Account acc
where
(Id NOT IN (select AccountID from opportunity
where AutoCreatedFlag__c = true ))]){
if(a.Service_End_Date__c!=NULL){
Integer numberDaysDue= d.daysBetween(a.Service_End_Date__c);
system.debug('Chidanand'+numberDaysDue);
if(numberDaysDue<=60) {
Opportunity Op= new Opportunity();
op.Name=a.Name;
op.StageName='Legal';
op.Amount=a.ARR__c;
op.AccountId=a.ID;
op.CloseDate=Date.today();
op.Customer_Success_Manager__c=a.Customer_Success_Manager__c;
op.NextStep='Won';
// op.OwnerID='005d0000001Kt9D';
op.AutoCreatedFlag__c = true;
insert op;
System.debug('Hi Chidanand');
// Id pricebookId = Test.getStandardPricebookId();
List<PriceBookEntry> entryList = new List<PriceBookEntry>();
Pricebook2 stdpb = [SELECT Id FROM Pricebook2 WHERE IsStandard = true]; // Select Price Book
PricebookEntry standardPrice = new PricebookEntry(
Pricebook2Id = stdpb .Id, Product2Id = prod.Id,
UnitPrice = 10, IsActive = true);
// entryList.add(standardPrice );
Integer counter = [select count() FROM PriceBookEntry where Product2.Id=:prod.Id And
PriceBook2.isStandard=true AND UnitPrice=10];
entryList.add(standardPrice );
if(counter==0){
insert entryList;
}
// insert entryList;
String z=prod.Id;
List<PriceBookEntry> priceBookList = [select Id, Product2Id, Product2.Id, Product2.Name FROM PriceBookEntry
where (Product2.Id=:z) And (PriceBook2.isStandard=true) LIMIT 1 ]; // Get the Price Book entry ID that we entered in the previous step
OpportunityLineItem oli = new OpportunityLineItem(); // Create new Opportunity Product
oli.OpportunityId = op.Id;
oli.PricebookEntryId=priceBookList[0].Id; // PricebookentryID of the existing pricebook entry for the specific product
oli.UnitPrice =10; // Sales Price (or List Price)
oli.Quantity=1;
insert oli;
}
}
}
}
}
APEX CODE
global class RenewalOpportunity implements Schedulable {
global void execute(SchedulableContext SC) {
RenewalOpp R= new RenewalOpp();
}
public class RenewalOpp{
RenewalOpp(){
Date d = Date.Today();
for(Account a:[select id,Name,Service_End_Date__c,Customer_Success_Manager__c,ARR__c
from Account acc
where
(Id NOT IN (select AccountID from opportunity
where AutoCreatedFlag__c = true ))])
{
if(a.Service_End_Date__c!=NULL){
// Difference between Service End date and Current date
Integer numberDaysDue= d.daysBetween(a.Service_End_Date__c);
if(numberDaysDue<=60) {
// Creating a new opportunity
Opportunity O= new Opportunity();
o.Name=a.Name;
o.StageName='Legal';
o.Amount=a.ARR__c;
o.AccountId=a.ID;
o.CloseDate=a.Service_End_Date__c;
o.Customer_Success_Manager__c=a.Customer_Success_Manager__c;
o.NextStep='Won';
o.OwnerID='005d0000001Kt9D';
o.AutoCreatedFlag__c = true;
insert o;
List<PriceBookEntry> priceBookList = [select Id, Product2Id, Product2.Id, Product2.Name FROM PriceBookEntry where Product2.Id='01td0000003SDB8' And
PriceBook2.isStandard=true LIMIT 1 ]; // Get the Price Book entry ID that we entered in the previous step
OpportunityLineItem oli = new OpportunityLineItem(); // Create new Opportunity Product
oli.OpportunityId = o.Id;
oli.PricebookEntryId=priceBookList[0].Id; // PricebookentryID of the existing pricebook entry for the specific product
oli.UnitPrice =a.ARR__c; // Sales Price (or List Price)
oli.Quantity=1;
insert oli;
}
}
}
}
}
}
Thanks a lot. I covered 100% code. But one question really wanna ask u.
Why did u put the Test.startTest() and Test.stopTest at those two specific Places ?
I really wanna Know.
Thanks