You need to sign in to do that
Don't have an account?
Apex Test coverage
Hi folks,
I am getting problem with test coverage.
I am getting test coverage of 0%
Actually i don't know how to link the test class with the Apex class.
Here is my 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=Date.today();
o.Customer_Success_Manager__c=a.Customer_Success_Manager__c;
o.NextStep='Won';
o.OwnerID='005d0000001Kt9D';
o.AutoCreatedFlag__c = true;
insert o;
// Create a Pricebook Entry for Product
List<OpportunityLineItem> oliList = new List <OpportunityLineItem>();
Pricebook2 stdpb = [SELECT Id FROM Pricebook2 WHERE IsStandard = true]; // Select Price Book
List<PriceBookEntry> entryList = new List<PriceBookEntry>();
PriceBookEntry pe= new PriceBookEntry();
pe.Pricebook2Id=stdpb.Id;
pe.Product2Id='01tJ0000003qkr0';
pe.UnitPrice=10; // Standard Price
pe.IsActive=true;
Integer counter = [select count() FROM PriceBookEntry where Product2.Id='01tJ0000003qkr0' And
PriceBook2.isStandard=true AND UnitPrice=10];
entryList.add(pe);
if(counter==0){
insert entryList;
}
List<PriceBookEntry> priceBookList = [select Id, Product2Id, Product2.Id, Product2.Name FROM PriceBookEntry where Product2.Id='01tJ0000003qkr0' 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;
}
}
}
}
}
}
My Test Code
@isTest (seeAllData=true)
public class RenewalTest{
static testmethod void RenewalTest() {
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);
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;
// 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;
}
}
}
}
}
I am getting problem with test coverage.
I am getting test coverage of 0%
Actually i don't know how to link the test class with the Apex class.
Here is my 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=Date.today();
o.Customer_Success_Manager__c=a.Customer_Success_Manager__c;
o.NextStep='Won';
o.OwnerID='005d0000001Kt9D';
o.AutoCreatedFlag__c = true;
insert o;
// Create a Pricebook Entry for Product
List<OpportunityLineItem> oliList = new List <OpportunityLineItem>();
Pricebook2 stdpb = [SELECT Id FROM Pricebook2 WHERE IsStandard = true]; // Select Price Book
List<PriceBookEntry> entryList = new List<PriceBookEntry>();
PriceBookEntry pe= new PriceBookEntry();
pe.Pricebook2Id=stdpb.Id;
pe.Product2Id='01tJ0000003qkr0';
pe.UnitPrice=10; // Standard Price
pe.IsActive=true;
Integer counter = [select count() FROM PriceBookEntry where Product2.Id='01tJ0000003qkr0' And
PriceBook2.isStandard=true AND UnitPrice=10];
entryList.add(pe);
if(counter==0){
insert entryList;
}
List<PriceBookEntry> priceBookList = [select Id, Product2Id, Product2.Id, Product2.Name FROM PriceBookEntry where Product2.Id='01tJ0000003qkr0' 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;
}
}
}
}
}
}
My Test Code
@isTest (seeAllData=true)
public class RenewalTest{
static testmethod void RenewalTest() {
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);
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;
// 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;
}
}
}
}
}
In terms of how to link your test to your class, it seems your test will need to actually call constructor on RenawalOpp in order to get that logic to execute. That is, calling
from your test case this will cause your logic to run, creating renewal opps for accounts within 60 days of expiration and adding coverage to lines in your class.
That said, running through the code I see a couple of issues that you may want to address in order to make this job ready for handling large numbers of records. I would suggest reading some resources on the trailhead to get acquainted with limits and why queries inside of loops are sucepable to failure. This page is talking about triggers, but the same bulk-thinking applies to a scheduled job: https://developer.salesforce.com/trailhead/apex_triggers/apex_triggers_bulk . In your case, if you have a lot of records to run thourgh, you might want to use Database.batchable instead. This book also covers these sorts of concepts in depth: http://advancedapex.com/
Beyond that, the use of seeAllData and hard-coded IDs should be avoided unless you have a very strong case for doing so.
Your suggestion of adding new RenewalOpp() in test class din't work for me. Could u plz suggest the other way around?
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_scheduler.htm
And like mentioned before, best practice is not to use seeAllData=true nor use hard coded Id's.
https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods