+ Start a Discussion
ChermaduraiChermadurai 

My testcase covered Only 60% Pls..help me to reach above 75%

My Class is:

 

global class FBR_ApexSchedulerMonthlyClass Implements Schedulable
{
global void execute(SchedulableContext sc)
{
ValAdd();
}
public static void ValAdd()//Set<Id> InquiryId
{

List<FBR_Inquiry__c> Inquiry= [SELECT KPIInquiryContent__c,KPIInquiryContent__r.Account__c,CommodityName__c,ModifyDate__c,Status__c from FBR_Inquiry__c ];
{
FBR_Inquiry__c FBR= Inquiry[0];
Date startDate=FBR.ModifyDate__c.toStartOfMonth();
Date endDate=FBR.ModifyDate__c.toStartOfMonth().addMonths(1);

LIST<AggregateResult> countmonth=[SELECT count(KPIInquiryContent__c) coun from FBR_Inquiry__c where KPIInquiryContent__r.Account__c=:FBR.KPIInquiryContent__r.Account__c and ModifyDate__c >= :startDate AND ModifyDate__c < :endDate and PayoffObject__c=:'○' and Status__c IN('対応中' ,'対応完了')];
//LIST<AggregateResult> calmonth=[Select CALENDAR_MONTH(WorkDate__c), count(ImportCount__c) icount from WorkResultsMonth__c where Account_Name__c=:acname GROUP BY CALENDAR_MONTH(WorkDate__c)];
LIST<AggregateResult> calmonth=[Select sum(ImportCount__c) icount from WorkResultsMonth__c where WorkDate__c >= :startDate and WorkDate__c < :endDate and Account_Name__c=:FBR.KPIInquiryContent__r.Account__c and name='dfjg'];

List<KPIAccident__c> acc= [SELECT MonthlyGeneratedCount__c,MonthlyPPM__c from KPIAccident__c where Account__c=:FBR.KPIInquiryContent__r.Account__c];
if(!acc.isEmpty() )
{

KPIAccident__c accupdate = acc[0];
double totalcountmonth=double.Valueof(countmonth[0].get('coun'));

accupdate.MonthlyGeneratedCount__c=totalcountmonth;

double totalmonthwork= double.Valueof(calmonth[0].get('icount'));
accupdate.MonthlyPPM__c=(totalcountmonth/totalmonthwork)*1000000;
update accupdate;
}
else
{
KPIAccident__c kpi = new KPIAccident__c();
List<KPIAccident__c> ToUpdate = new List<KPIAccident__c>();
// kpi.Date__c=FBR.ModifyDate__c;
kpi.Account__c=FBR.KPIInquiryContent__r.Account__c;
// kpi.itemname__c= FBR.CommodityName__c;

double totalcountmonths=double.Valueof(countmonth[0].get('coun'));
kpi.MonthlyGeneratedCount__c=totalcountmonths;
double totalmonthworks= double.Valueof(calmonth[0].get('icount'));

kpi.MonthlyPPM__c=(totalcountmonths/totalmonthworks)*1000000;
ToUpdate.add(kpi);
insert ToUpdate;

}
/
}
} }

 

 

My Test Case  is:

 

@isTest(SeeAllData=true)
class FBR_ApexScheduleMonthClassTest {
static testMethod void myUnitTest() {
test.startTest();
// FBR_ApexSchedulerMonthlyClass.valadd();
account a = new account();
a.name='saaa';
insert a;
KPI_StoreInquiryStatus__c a3 = new KPI_StoreInquiryStatus__c(Account__c=a.id,name='asdf');
insert a3;
KPIInquiryStatus__c a4 = new KPIInquiryStatus__c();
a4.Account__c=a.id;
insert a4;

FBR_Inquiry__c fi=new FBR_Inquiry__c();

fi.KPIInquiryContent__c=a3.id;
fi.KpiInquiryRef__c=a4.id;
fi.CommodityName__c='fgggggj';
fi.ModifyDate__c=date.parse('2012/10/10');
fi.Status__c ='hkjcyh';
insert fi;


Double tot=0;
//WorkResultsMonth__c wk= new WorkResultsMonth__c (Account_Name__c=a.id, WorkDate__c = System.today());
// insert wk;
// LIST<AggregateResult> calmonth=[SELECT sum(ImportCount__c) from WorkResultsMonth__c where WorkDate__c = TODAY and Account_Name__c= :a.id] ;
WorkResultsMonth__c wk=new WorkResultsMonth__c();
wk.WorkDate__c =date.parse('2012/10/10');
wk.Account_Name__c=a.id;
wk.ImportCount__c=10;
insert wk;

List<KPIAccident__c> acc= new List<KPIAccident__c>();
acc=[SELECT MonthlyGeneratedCount__c,MonthlyPPM__c from KPIAccident__c where Account__c=:a.id];

KPIAccident__c kpi = new KPIAccident__c();
kpi.MonthlyGeneratedCount__c=tot;
kpi.MonthlyPPM__c=456;
acc.add(kpi);

List<KPIAccident__c> acc2= new List<KPIAccident__c>();
KPIAccident__c kpi2 = new KPIAccident__c();
kpi2.Account__c=a.id;
kpi2.MonthlyGeneratedCount__c=tot;
kpi2.MonthlyPPM__c=147;
acc.add(kpi2); 
insert acc;
 update acc2;


FBR_ApexSchedulerMonthlyClass Updates = new FBR_ApexSchedulerMonthlyClass();
String schedule = '0 0 19 * * ?';
system.schedule('Nightly Update', schedule, Updates );


test.stopTest();
}
}

 

 

The Else Part doesnt covered in test case..

salvatoreovlassalvatoreovlas

just write another test method where the condition is not verified and it will go through your else.