I am trying to write a test class for the Apex controller but I am receiving below error. "System.MathException: Divide by 0"
Apex Controller:
public class controller{
public Id accountId{get;set;}
public Integer getStatusNew() {
Integer count1 = 0;
count1 = [SELECT count() FROM Account WHERE Status__c = 'New' AND change_date__c = LAST_WEEK ];
return count1;
}
public Integer getStatusProcess() {
Integer count2 = 0;
count1 = [SELECT count() FROM Account WHERE Status__c = 'Process' AND change_date__c = LAST_WEEK ];
return count2;
}
public Integer getStatusOld() {
Integer count3 = 0;
count1 = [SELECT count() FROM Account WHERE Status__c = 'Old' AND change_date__c = LAST_WEEK ];
return count3;
}
//I have updated change_date__c to LAST_N_WEEKS:2 and added three more statuses
public double getcountNew(){ //And I have added three more methods for other statuses with similar Logic
double c1 = 0;
double c2 = 0;
c1 = [SELECT count() FROM Account WHERE Status__c = 'New' AND change_date__c = LAST_WEEK ];
c2 = [SELECT count() FROM Account WHERE Status__c = 'New' AND change_date__c = LAST_N_WEEKS:2 ];
return (((c1 - c2)/c2)/100);
}
public string getDate1(){
Date d = System.Today();
Datetime dt = d.toStartofWeek() - 7;
String LastWeek = dt.format('MM-dd-yyyy');
return LastWeek ;}
}
}
I had to follow this approach because my VisualForce component output is a table
status Last week(date) Last two weeks(date) Percentage
New Count of acc wid this status count calculation result
Old count count calculaion Result
Process count count calculaion Result
Test Class:
@isTest()
private class Controller_test{
static testMethod void getStatus(){
Account acc= new Account(Name='Test', Status__c='New' );
//Inserted accounts with all the statuses
insert acc;
partnerStatus classObject = new partnerStatus();
classObject.accountId = acc.Id;
Integer newStatus = classObject.getStatusNew();
//added variables for all the statuses
Double count1 = classobject.getCountNew(); //Error
//added assert for all variables
system.assertNotEquals(0, count1); // Error
system.assertNotEquals(1, newStatus);
}
}
I am using this controller in apex component, to display the Percentage of the count, and to get the Last week date in the component. I am unable to test the getcountNew() and getDate1() methods. How can I add this is in test class. Thanks.
Modified Test class:
@isTest()
private class Controller_test{
static testMethod void getStatus(){
Account acc= new Account(Name='Test', Partner_Lead_Status__c='New' , Lead_status_change_date__c = Date.today().addDays(-3));
Account acc2= new Account(Name='Test2', Partner_Lead_Status__c='Prospect', Lead_status_change_date__c = Date.today().addDays(-9));
insert acc;
insert acc2;
partnerStatus classObject = new partnerStatus();
classObject.accountId = acc.Id;
classObject.accountId = acc2.Id;
Integer newStatus = classObject.getStatusNew();
Double calResult = classobject.getCountNew();
system.assertNotEquals(0, calResult);
system.assertNotEquals(1, newStatus);
}
}
and my code looks very lengthy as well, can you guide me if there is any other way to write this code. Thanks!