You need to sign in to do that
Don't have an account?
raman123
How can i write test class for it .
public class ExtensionVoucherHandler {
public static Boolean doRun = True;
public static void VoucherResend(Map <id,Certification_Voucher__c> oldMap, list<Certification_Voucher__c> newlist ){
map<string,Certification_Voucher__c> codemap = new map<string,Certification_Voucher__c>();
for(Certification_Voucher__c C : newlist){
if(C.Expiration_Date__c != oldMap.get(C.id).Expiration_Date__c){
codemap.put(C.name,C);
}
}
List<Training_Attendee__c> TA = [select id,Voucher_Code__c,Voucher_Expiration_Date__c from Training_Attendee__c where Voucher_Code__c in: codemap.keyset()];
List<Training_Attendee__c> lstUpdate = new List<Training_Attendee__c>();
for(Training_Attendee__c train : TA){
if(codemap.containsKey(train.Voucher_Code__c)){
train.Voucher_Expiration_Date__c = codemap.get(train.Voucher_Code__c).Formatted_Expiration_Date__c ;
lstUpdate.add(train);
}
}
if(lstupdate != null && lstupdate.size() > 0)
database.update (lstUpdate,false);
}
}
public static Boolean doRun = True;
public static void VoucherResend(Map <id,Certification_Voucher__c> oldMap, list<Certification_Voucher__c> newlist ){
map<string,Certification_Voucher__c> codemap = new map<string,Certification_Voucher__c>();
for(Certification_Voucher__c C : newlist){
if(C.Expiration_Date__c != oldMap.get(C.id).Expiration_Date__c){
codemap.put(C.name,C);
}
}
List<Training_Attendee__c> TA = [select id,Voucher_Code__c,Voucher_Expiration_Date__c from Training_Attendee__c where Voucher_Code__c in: codemap.keyset()];
List<Training_Attendee__c> lstUpdate = new List<Training_Attendee__c>();
for(Training_Attendee__c train : TA){
if(codemap.containsKey(train.Voucher_Code__c)){
train.Voucher_Expiration_Date__c = codemap.get(train.Voucher_Code__c).Formatted_Expiration_Date__c ;
lstUpdate.add(train);
}
}
if(lstupdate != null && lstupdate.size() > 0)
database.update (lstUpdate,false);
}
}
How are you calling this class? Via trigger or via some other class. I have called this class via trigger (after update) and it is giving me 100% code coverage.
Please see the trigger below:
trigger testVouchertrigger on Certification_Voucher__c (after update) {
ExtensionVoucherHandler.VoucherResend(trigger.oldmap,trigger.new);
}
Test Class:
@isTest(SeeAllData=false)
public class ExtensionVoucherHandlerTest {
@istest static void testSuccess(){
Certification_Voucher__c vou = new Certification_Voucher__c();
vou.Expiration_Date__c =System.today();
vou.name = 'test';
// add all required fields
insert vou ;
vou.Expiration_Date__c =System.today()+5;
Update vou;
Training_Attendee__c att = new Training_Attendee__c() ;
//add all required fields
att.Voucher_Code__c =vou.name ;
insert att ;
vou.Expiration_Date__c =System.today()+10 ;
update vou ;
}
}
All Answers
What kind of field is this " Voucher_Code__c" ? is it a relationship field? or just a text field?
Thats why the code provided by @Raj Vakati is giving you 0% coverage. Because in the test class Voucher_Code__c is passed as id (like the way we do for a relationship field) and you have name added in your Map otherwise the test class is perfectly fine. Just did a small change.
@isTest(SeeAllData=false)
public class ExtensionVoucherHandlerTest {
@istest static void testSuccess(){
Certification_Voucher__c vou = new Certification_Voucher__c();
vou.Expiration_Date__c =System.today();
vou.name = 'test';
// add all required fields
insert vou ;
Training_Attendee__c att = new Training_Attendee__c() ;
//add all required fields
att.Voucher_Code__c =vou.name ;
insert att ;
vou.Expiration_Date__c =System.today()+10 ;
update vou ;
}
}
Try this code.
How are you calling this class? Via trigger or via some other class. I have called this class via trigger (after update) and it is giving me 100% code coverage.
Please see the trigger below:
trigger testVouchertrigger on Certification_Voucher__c (after update) {
ExtensionVoucherHandler.VoucherResend(trigger.oldmap,trigger.new);
}
Test Class:
@isTest(SeeAllData=false)
public class ExtensionVoucherHandlerTest {
@istest static void testSuccess(){
Certification_Voucher__c vou = new Certification_Voucher__c();
vou.Expiration_Date__c =System.today();
vou.name = 'test';
// add all required fields
insert vou ;
vou.Expiration_Date__c =System.today()+5;
Update vou;
Training_Attendee__c att = new Training_Attendee__c() ;
//add all required fields
att.Voucher_Code__c =vou.name ;
insert att ;
vou.Expiration_Date__c =System.today()+10 ;
update vou ;
}
}
Try this:
@isTest
private class ExtensionVoucherHandler_Test {
@isTest static void testExtensionVoucherHandle() {
List<Certification_Voucher__c> cvObjList = new List<Certification_Voucher__c>();
Certification_Voucher__c cvObj = new Certification_Voucher__c();
cvObj.Name = 'Test';
cvObj.Expiration_Date__c = System.today();
cvObj.Formatted_Expiration_Date__c = System.today() + 1;
//put all required field here
cvObjList.add(cvObj);
insert cvObjList;
System.assertNotEquals(cvObjList[0].Id, null,'Assertion Failed : Record Not Inserted');
map<Id, Certification_Voucher__c> demoMap = new map<Id, Certification_Voucher__c>();
if(!demoMap.containsKey(cvObjList[0].Id))
{
demoMap.put(cvObjList[0].Id, cvObjList[0]);
}
List<Certification_Voucher__c> cvObjListT = [Select Id, Name, Expiration_Date__c, Formatted_Expiration_Date__c From Certification_Voucher__c Where Id In: cvObjList];
for(Certification_Voucher__c cv : cvObjListT) {
cv.Expiration_Date__c = Date.newInstance(2020, 06, 05);
}
update cvObjListT;
Training_Attendee__c taObj = new Training_Attendee__c();
taObj.Name = 'Test';
taObj.Voucher_Code__c = 'Test';
taObj.Voucher_Expiration_Date__c = System.today() + 3;
//put all required field here
insert taObj;
System.assertNotEquals(taObj.Id, null,'Assertion Failed-- : Record Not Inserted');
ExtensionVoucherHandler.VoucherResend(demoMap, cvObjListT);
}
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi