You need to sign in to do that
Don't have an account?
sumit pawar
how to get 100% test coverage for apex class?
I tried to get up below class coverage from 62% to 100% but no luck..
what i am missing...?Please help me out...bold code in test class has not covered...
Apex class:
public with sharing class InvoiceGenerator {
public void generateInvoices() {
Invoice_Generation_Frequency__c igf = Invoice_Generation_Frequency__c.getInstance('Default');
system.debug('igf.Days__c:'+igf.Days__c);
Integer currentYear = system.today().year();
Integer currentDay = (system.today().addDays((integer)igf.Days__c)).day();
system.debug('--day--'+currentDay);
List<Account> accList = [Select Id,Group__c,Group_Signup_Start_Date__c,Group_Renewal_Date__c,Invoice_Amount__c From Account where
Group__c='yes' and CALENDAR_YEAR(Group_Renewal_Date__c) < = :currentYear and DAY_IN_MONTH(Group_Renewal_Date__c) = :currentDay];
String currYear = system.today().year() + '%';
//List<AggregateResult> arList = [select count(id) cnt from Invoice__c where Invoice_Number__c like :currYear];
//nextInvNum = (integer)arList[0].get('cnt');
List<Invoice__c> invnumberList = [select Invoice_Number__c from Invoice__c where Invoice_Number__c like:currYear order by Invoice_Number__c desc];
List<Invoice__c> InvoiceList = new List<Invoice__c>();
if(invnumberList.size() > 0){
string invhighestnum = invnumberList[0].Invoice_Number__c;
string convertedlast = invhighestnum.substring(5,9);
integer InvNumhighest = integer.valueOf(convertedlast);
system.debug('132654'+InvNumhighest);
system.debug('--InvNumhighestnew--'+InvNumhighest);
for(Account a : accList) {
InvNumhighest++;
Invoice__c Inv = new Invoice__c();
Inv.Account__c = a.Id;
Inv.Paid__c = false;
Inv.Group_Start_Date__c = a.Group_Signup_Start_Date__c;
Inv.Group_Renewal_Date__c = a.Group_Renewal_Date__c;
Inv.Invoice_Amount__c = a.Invoice_Amount__c;
Inv.Invoice_Number__c = (system.today().year() + '-' + InvNumhighest);
InvoiceList.add(Inv);
system.debug('--invList--'+InvoiceList);
}
insert InvoiceList;
}
else {
integer nextInvNum;
nextInvNum = 0000;
nextInvNum = nextInvNum + 2000;
system.debug('--nextInvNum--'+nextInvNum);
for(Account a : accList) {
nextInvNum++;
Invoice__c Inv = new Invoice__c();
Inv.Account__c = a.Id;
Inv.Paid__c = false;
Inv.Start_Date__c = a.Group_Signup_Start_Date__c;
Inv.Renewal_Date__c = a.Group_Renewal_Date__c;
Inv.Invoice_Amount__c = a.Invoice_Amount__c;
Inv.Invoice_Number__c = (system.today().year() + '-' + nextInvNum);
InvoiceList.add(Inv);
}
insert InvoiceList;
}
}
}
Test class:
@isTest(SeeAllData=true)
class TestInvoiceUpdate{
public static String CRON_EXP = '0 00 01 * * ?';
static testmethod void test() {
Test.startTest();
Account a = new Account();
a.Name = 'test';
a.Group__c = 'Yes';
Date myDate = date.newinstance((system.today().addYears(-1)).year(), (system.today().addDays(45)).month(), (system.today().addDays(45)).day());
a.Group_Renewal_Date__c = myDate;
insert a;
system.debug('Myinsert'+a);
Invoice__c i = new Invoice__c();
i.Paid__c = false;
i.Account__c = a.id;
i.Invoice_Number__c = '1111';
insert i;
String InvId = System.schedule('testBasicScheduledApex',CRON_EXP,new InvoiceUpdate());
// Get the information from the CronTrigger API object
CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :InvId];
// Verify the expressions are the same
System.assertEquals(CRON_EXP, ct.CronExpression);
// Verify the job has not run
System.assertEquals(0, ct.TimesTriggered);
Test.stopTest();
}
static testmethod void test1() {
Test.startTest();
Account a = new Account();
a.Name = 'test1';
a.Group__c = 'Yes';
Date myDate1 = date.newinstance((system.today().addYears(-1)).year(), (system.today().addDays(45)).month(), (system.today().addDays(45)).day());
a.Group_Renewal_Date__c = myDate1;
insert a;
system.debug('Myinsert'+a);
Invoice__c i = new Invoice__c();
i.Paid__c = false;
i.Account__c = a.id;
i.Invoice_Number__c = '2222';
insert i;
String InvId = System.schedule('testBasicScheduledApex',CRON_EXP,new InvoiceUpdate());
// Get the information from the CronTrigger API object
CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :InvId];
// Verify the expressions are the same
System.assertEquals(CRON_EXP, ct.CronExpression);
// Verify the job has not run
System.assertEquals(0, ct.TimesTriggered);
Test.stopTest();
}
}
what i am missing...?Please help me out...bold code in test class has not covered...
Apex class:
public with sharing class InvoiceGenerator {
public void generateInvoices() {
Invoice_Generation_Frequency__c igf = Invoice_Generation_Frequency__c.getInstance('Default');
system.debug('igf.Days__c:'+igf.Days__c);
Integer currentYear = system.today().year();
Integer currentDay = (system.today().addDays((integer)igf.Days__c)).day();
system.debug('--day--'+currentDay);
List<Account> accList = [Select Id,Group__c,Group_Signup_Start_Date__c,Group_Renewal_Date__c,Invoice_Amount__c From Account where
Group__c='yes' and CALENDAR_YEAR(Group_Renewal_Date__c) < = :currentYear and DAY_IN_MONTH(Group_Renewal_Date__c) = :currentDay];
String currYear = system.today().year() + '%';
//List<AggregateResult> arList = [select count(id) cnt from Invoice__c where Invoice_Number__c like :currYear];
//nextInvNum = (integer)arList[0].get('cnt');
List<Invoice__c> invnumberList = [select Invoice_Number__c from Invoice__c where Invoice_Number__c like:currYear order by Invoice_Number__c desc];
List<Invoice__c> InvoiceList = new List<Invoice__c>();
if(invnumberList.size() > 0){
string invhighestnum = invnumberList[0].Invoice_Number__c;
string convertedlast = invhighestnum.substring(5,9);
integer InvNumhighest = integer.valueOf(convertedlast);
system.debug('132654'+InvNumhighest);
system.debug('--InvNumhighestnew--'+InvNumhighest);
for(Account a : accList) {
InvNumhighest++;
Invoice__c Inv = new Invoice__c();
Inv.Account__c = a.Id;
Inv.Paid__c = false;
Inv.Group_Start_Date__c = a.Group_Signup_Start_Date__c;
Inv.Group_Renewal_Date__c = a.Group_Renewal_Date__c;
Inv.Invoice_Amount__c = a.Invoice_Amount__c;
Inv.Invoice_Number__c = (system.today().year() + '-' + InvNumhighest);
InvoiceList.add(Inv);
system.debug('--invList--'+InvoiceList);
}
insert InvoiceList;
}
else {
integer nextInvNum;
nextInvNum = 0000;
nextInvNum = nextInvNum + 2000;
system.debug('--nextInvNum--'+nextInvNum);
for(Account a : accList) {
nextInvNum++;
Invoice__c Inv = new Invoice__c();
Inv.Account__c = a.Id;
Inv.Paid__c = false;
Inv.Start_Date__c = a.Group_Signup_Start_Date__c;
Inv.Renewal_Date__c = a.Group_Renewal_Date__c;
Inv.Invoice_Amount__c = a.Invoice_Amount__c;
Inv.Invoice_Number__c = (system.today().year() + '-' + nextInvNum);
InvoiceList.add(Inv);
}
insert InvoiceList;
}
}
}
Test class:
@isTest(SeeAllData=true)
class TestInvoiceUpdate{
public static String CRON_EXP = '0 00 01 * * ?';
static testmethod void test() {
Test.startTest();
Account a = new Account();
a.Name = 'test';
a.Group__c = 'Yes';
Date myDate = date.newinstance((system.today().addYears(-1)).year(), (system.today().addDays(45)).month(), (system.today().addDays(45)).day());
a.Group_Renewal_Date__c = myDate;
insert a;
system.debug('Myinsert'+a);
Invoice__c i = new Invoice__c();
i.Paid__c = false;
i.Account__c = a.id;
i.Invoice_Number__c = '1111';
insert i;
String InvId = System.schedule('testBasicScheduledApex',CRON_EXP,new InvoiceUpdate());
// Get the information from the CronTrigger API object
CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :InvId];
// Verify the expressions are the same
System.assertEquals(CRON_EXP, ct.CronExpression);
// Verify the job has not run
System.assertEquals(0, ct.TimesTriggered);
Test.stopTest();
}
static testmethod void test1() {
Test.startTest();
Account a = new Account();
a.Name = 'test1';
a.Group__c = 'Yes';
Date myDate1 = date.newinstance((system.today().addYears(-1)).year(), (system.today().addDays(45)).month(), (system.today().addDays(45)).day());
a.Group_Renewal_Date__c = myDate1;
insert a;
system.debug('Myinsert'+a);
Invoice__c i = new Invoice__c();
i.Paid__c = false;
i.Account__c = a.id;
i.Invoice_Number__c = '2222';
insert i;
String InvId = System.schedule('testBasicScheduledApex',CRON_EXP,new InvoiceUpdate());
// Get the information from the CronTrigger API object
CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :InvId];
// Verify the expressions are the same
System.assertEquals(CRON_EXP, ct.CronExpression);
// Verify the job has not run
System.assertEquals(0, ct.TimesTriggered);
Test.stopTest();
}
}
It is difficult to see all the code can u pls tell me ur requirment for what u have written the class it helps me to understand the code
thanks,
pradeep
here we are creating invoices in invoice object...
first if condtion is get highest invoice number and increment it for next invoices...
and else condition is for if invoice list is null then it will increment...
Here as u said first if condition is to check the highest invoice number and if it is greateg than 0 ur doing some operation BUT u have written the same code for both IF and ELSE condition as u said for the else conditon to execute there should not be any invoice records that means u have to insert a insert a invoice record and retrive the record with a false condition that means the record will not be fetched and the size of the list will be 0 so the else condition will execute.
Thanks,
pradeep.
List<Invoice__c> invnumberList = [select Invoice_Number__c from Invoice__c where Invoice_Number__c like:currYear order by Invoice_Number__c desc];
you are retriving data into this list to make this list not to get even a single record for the data base means u have written Invoice_Number__c like xxxxxxxxxxx
so just dont give the in voice no thats it
tell me if u face further problem..
Thanks,
pradeep.
i.Paid__c = false;
i.Account__c = a.id;
i.Invoice_Number__c = '2222';(DONT INSERT THIS LINE IF THE FIELD IS MANDATORY GIVE SOME ALPHABETS AND INSERT THE RECORD )
insert i;