You need to sign in to do that
Don't have an account?
Vikas Menon
After update test class issue
Hi All,
Need Help !!... i have written a trigger which updates payments on change of status on account. Pasting code here. Somehow my test class does not give any coverage at all. Its urgent !
trigger :-
trigger CancelAdmission on Account(after update)
{
// if(system.isFuture()) return;
if(checkRecursive.ischeckcanceltrigger==true)
{
checkRecursive.ischeckcanceltrigger=false;
}
else
{
return;
}
Set<Id> accountIds = new Set<Id>();
Set<Id> oppsIds = new Set<Id>();
List<Lead> reverselead = new list<Lead>();
List<nmRefund_Payment__c> refund = new List<nmRefund_Payment__c>();
String strEmailid;
for(Account ac : Trigger.new)
{
system.debug('<-----------------nm_StudentStatus__c------------>'+ac.nm_StudentStatus__c);
if(ac.nm_StudentStatus__c=='Admission Cancelled' || ac.nm_StudentStatus__c=='Registration Cancelled')
{
accountIds.add(ac.Id);
}
system.debug('<--------------------------accountIds-------------->'+accountIds);
}
if(accountIds.size()>0)
{
//List<Deduction_Amount__c> daupdate = new List<Deduction_Amount__c>();
List<Opportunity> lstAppOpportuniyties = new List<Opportunity>();
list<Opportunity> lstUpdate=new list<Opportunity>();
list<nmRefund_Payment__c> lstRefundpayment=new list<nmRefund_Payment__c>();
map<String,Opportunity> mapsemWise=new map<String,Opportunity>();
lstAppOpportuniyties=[select id,Account.Is_Lateral_c__c,nm_InformationCenters__c,Account.PersonEmail,nm_Program__r.nm_Type__c,Amount,StageName,nm_Session__c,nm_Semester__c,nm_StudentSession__c,AccountId,Name,(select id,nm_ModeOfPayment__c,nm_Amount__c,nm_PaymentType__c,nm_PaymentStatus__c from Opportunities__r where (nm_PaymentType__c='Admission' OR nm_PaymentType__c='Late Fees')) from Opportunity where AccountId in: accountIds order by nm_Semester__c Desc];
list<Opportunity> lstToUpdate=new list<Opportunity>();
if(lstAppOpportuniyties.size()>0)
{
for(Opportunity objOpp:lstAppOpportuniyties)
{
mapsemWise.put(objOpp.nm_Semester__c,objOpp);
}
}
strEmailid=lstAppOpportuniyties[0].Account.PersonEmail;
if(lstAppOpportuniyties.size()>0)
{
Opportunity objopp=lstAppOpportuniyties[0];
String StrSemister=lstAppOpportuniyties[0].nm_Semester__c;
list<nm_Payment__c> lstPayment=new list<nm_Payment__c>();
//Sem 1
if((lstAppOpportuniyties[0].StageName=='Closed Won' || lstAppOpportuniyties[0].StageName =='Closed'))
{
Integer intTotalMountTorefund=0;
String ModeofPaymentSem1;
lstToUpdate.add(lstAppOpportuniyties[0]);
integer intLatefees=0;
if(lstToUpdate.size()>0)
{
for(opportunity objOppGetAmountsem1:lstToUpdate)
{
for(nm_Payment__c objPayment:objOppGetAmountsem1.Opportunities__r)
{
if(objPayment.nm_PaymentType__c=='Admission' && (objPayment.nm_PaymentStatus__c=='Payment Made' || objPayment.nm_PaymentStatus__c=='Payment Approved'))
{
ModeofPaymentSem1=objPayment.nm_ModeOfPayment__c;
intTotalMountTorefund+=Integer.valueof(objPayment.nm_Amount__c);
}
if(objPayment.nm_PaymentType__c=='Late Fees' && (objPayment.nm_PaymentStatus__c=='Payment Made' || objPayment.nm_PaymentStatus__c=='Payment Approved'))
{
intLatefees+=Integer.valueof(objPayment.nm_Amount__c);
}
}
}
}
if(lstAppOpportuniyties[0].StageName=='Closed Won'){
nmRefund_Payment__c objRefundPayment=new nmRefund_Payment__c();
objRefundPayment.Total_Refund_Amount__c=intTotalMountTorefund;
objRefundPayment.nm_Payment_Status__c='Payment made';
// RP.nm_Amount__c=opp.nm_AmountReceived__c;
objRefundPayment.Account__c=lstAppOpportuniyties[0].Accountid;
objRefundPayment.Mode_Of_Payment__c=ModeofPaymentSem1;
//objRefundPayment.Additional_Deduction__c=2000;
objRefundPayment.nm_Information_Center__c=lstAppOpportuniyties[0].nm_InformationCenters__c;
lstRefundpayment.add(objRefundPayment);
}
//commented for closed cases
if(lstAppOpportuniyties[0].StageName=='Closed'){
nmRefund_Payment__c objRefundPayment=new nmRefund_Payment__c();
objRefundPayment.Total_Refund_Amount__c=intTotalMountTorefund+intLatefees;
objRefundPayment.nm_Payment_Status__c='Payment made';
// RP.nm_Amount__c=opp.nm_AmountReceived__c;
objRefundPayment.Account__c=lstAppOpportuniyties[0].Accountid ;
objRefundPayment.Mode_Of_Payment__c=ModeofPaymentSem1;
//objRefundPayment.Additional_Deduction__c=0;
objRefundPayment.nm_Information_Center__c=lstAppOpportuniyties[0].nm_InformationCenters__c;
lstRefundpayment.add(objRefundPayment);
}
}
if(lstToUpdate.size()>0)
{
for(Opportunity objOppUpdat:lstToUpdate)
{
if(objOppUpdat.StageName=='Closed')
{
objOppUpdat.StageName='Registration Cancelled';
}
else
{
objOppUpdat.StageName='Admission Cancelled';
}
}
}
update lstToUpdate;
}
if(strEmailid !=null && strEmailid !=''){
list<lead> objLead=[select id from lead where Email=:strEmailid and isConverted=true];
if(objLead.size()>0)
{
try{
delete objLead;
}
catch(Exception e)
{
}
}
}
set<id> stid=new set<id>();
if(lstRefundpayment.size()>0)
{
insert lstRefundpayment;
for(nmRefund_Payment__c objRfPay:lstRefundpayment)
{
stid.add(objRfPay.id);
}
}
if(accountIds.size()>0){
CancelFutureHandler.CancelAdmission(accountIds,stid);
}
}
}
<------------------------------------------------------------------------------------------------------------------------------------------------------------------------->
Test Class :-
@istest(SeeAllData=true)
public class CancelAdmissionTracker {
Public static nm_Centers__c objCen;
Public static User objUser;
Public static nm_UserMapping__c objUserMap;
Public static nm_Program__c objProgram;
Public static nm_WorkExperience__c objWrkExp;
public static nmRefund_Payment__c objRefundPayment;
public static Account objAccount;
public static Opportunity opps;
static testMethod void testmethod1()
{
loadData();
objAccount.nm_StudentStatus__c='Registration Cancelled'; ------> (the status does not get updated post inserting.)
Test.StartTest();
update objAccount;
Test.StopTest();
}
static void loadData()
{
// List<Opportunity> lstOpps= new List<Opportunity>();
List<nm_EligiblityCriteria__c> lstElegCritria= new List<nm_EligiblityCriteria__c>();
List<Account> lstAcc1= new List<Account>();
List<Lead> lstLead = new List<Lead>();
nm_EligiblityCriteria__c objEC = new nm_EligiblityCriteria__c();
CommonTrackerClass.createEligibiltyCriteria(objEC);
lstElegCritria.add(objEC);
System.debug('**eligibility'+objEC);
nm_EligiblityCriteria__c objEC1 = new nm_EligiblityCriteria__c();
CommonTrackerClass.createEligibiltyCriteria(objEC1);
lstElegCritria.add(objEC1);
Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];
objUser = new User(Alias = 'newUser', Email='newuser@testorg.com',
EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Los_Angeles', UserName='newuseoijior@testorg.com');
insert objUser;
Recordtype RT=[select id from Recordtype where sobjectType='nm_Centers__c' AND name ='Information Center'];
objCen= new nm_Centers__c();
objCen.RecordTypeID = RT.id;
objCen.nm_CenterCode__c = '002';
objCen.nm_CenterCity__c = 'Delhi';
objCen.nm_City__c = 'Delhi';
objCen.nm_StateProvince__c = 'Delhi';
objCen.nm_Street__c = 'Krishna Nagar';
objCen.nm_PostalCode__c = '110051';
// objCen.Owner=objUser;
insert objCen;
objUserMap= new nm_UserMapping__c();
objUserMap.nm_Centers__c=objCen.id;
objUserMap.nm_User__c=objUser.id;
objUserMap.nm_Phone__c='326392156';
objUserMap.nm_Head__c= true;
insert objUserMap;
objAccount= new Account();
objAccount.nm_PreStudentNo__c ='77213';
objAccount.nm_StudentStatus__c='Confirmed';
objAccount.nm_SerialNo__c=2;
objAccount.nm_IsDocumentApproved__c = true;
objAccount.nm_Centers__c=objCen.id;
objAccount.lastname ='singh';
objAccount.FirstName='Chandra';
objAccount.nm_AccountEmail__c='chandra@gmail.com';
objAccount.lastname ='kumar';
objAccount.PersonMobilePhone='8377985721';
objAccount.PersonEmail='chandra@gmail.com';
objAccount.nm_EligiblityCriteria__c=objEC1.Id;
objAccount.nmCaptureOpportuntiyStage__c='Registration Done';
objAccount.ownerid = objUser.id;
insert objAccount;
}
}
Need Help !!... i have written a trigger which updates payments on change of status on account. Pasting code here. Somehow my test class does not give any coverage at all. Its urgent !
trigger :-
trigger CancelAdmission on Account(after update)
{
// if(system.isFuture()) return;
if(checkRecursive.ischeckcanceltrigger==true)
{
checkRecursive.ischeckcanceltrigger=false;
}
else
{
return;
}
Set<Id> accountIds = new Set<Id>();
Set<Id> oppsIds = new Set<Id>();
List<Lead> reverselead = new list<Lead>();
List<nmRefund_Payment__c> refund = new List<nmRefund_Payment__c>();
String strEmailid;
for(Account ac : Trigger.new)
{
system.debug('<-----------------nm_StudentStatus__c------------>'+ac.nm_StudentStatus__c);
if(ac.nm_StudentStatus__c=='Admission Cancelled' || ac.nm_StudentStatus__c=='Registration Cancelled')
{
accountIds.add(ac.Id);
}
system.debug('<--------------------------accountIds-------------->'+accountIds);
}
if(accountIds.size()>0)
{
//List<Deduction_Amount__c> daupdate = new List<Deduction_Amount__c>();
List<Opportunity> lstAppOpportuniyties = new List<Opportunity>();
list<Opportunity> lstUpdate=new list<Opportunity>();
list<nmRefund_Payment__c> lstRefundpayment=new list<nmRefund_Payment__c>();
map<String,Opportunity> mapsemWise=new map<String,Opportunity>();
lstAppOpportuniyties=[select id,Account.Is_Lateral_c__c,nm_InformationCenters__c,Account.PersonEmail,nm_Program__r.nm_Type__c,Amount,StageName,nm_Session__c,nm_Semester__c,nm_StudentSession__c,AccountId,Name,(select id,nm_ModeOfPayment__c,nm_Amount__c,nm_PaymentType__c,nm_PaymentStatus__c from Opportunities__r where (nm_PaymentType__c='Admission' OR nm_PaymentType__c='Late Fees')) from Opportunity where AccountId in: accountIds order by nm_Semester__c Desc];
list<Opportunity> lstToUpdate=new list<Opportunity>();
if(lstAppOpportuniyties.size()>0)
{
for(Opportunity objOpp:lstAppOpportuniyties)
{
mapsemWise.put(objOpp.nm_Semester__c,objOpp);
}
}
strEmailid=lstAppOpportuniyties[0].Account.PersonEmail;
if(lstAppOpportuniyties.size()>0)
{
Opportunity objopp=lstAppOpportuniyties[0];
String StrSemister=lstAppOpportuniyties[0].nm_Semester__c;
list<nm_Payment__c> lstPayment=new list<nm_Payment__c>();
//Sem 1
if((lstAppOpportuniyties[0].StageName=='Closed Won' || lstAppOpportuniyties[0].StageName =='Closed'))
{
Integer intTotalMountTorefund=0;
String ModeofPaymentSem1;
lstToUpdate.add(lstAppOpportuniyties[0]);
integer intLatefees=0;
if(lstToUpdate.size()>0)
{
for(opportunity objOppGetAmountsem1:lstToUpdate)
{
for(nm_Payment__c objPayment:objOppGetAmountsem1.Opportunities__r)
{
if(objPayment.nm_PaymentType__c=='Admission' && (objPayment.nm_PaymentStatus__c=='Payment Made' || objPayment.nm_PaymentStatus__c=='Payment Approved'))
{
ModeofPaymentSem1=objPayment.nm_ModeOfPayment__c;
intTotalMountTorefund+=Integer.valueof(objPayment.nm_Amount__c);
}
if(objPayment.nm_PaymentType__c=='Late Fees' && (objPayment.nm_PaymentStatus__c=='Payment Made' || objPayment.nm_PaymentStatus__c=='Payment Approved'))
{
intLatefees+=Integer.valueof(objPayment.nm_Amount__c);
}
}
}
}
if(lstAppOpportuniyties[0].StageName=='Closed Won'){
nmRefund_Payment__c objRefundPayment=new nmRefund_Payment__c();
objRefundPayment.Total_Refund_Amount__c=intTotalMountTorefund;
objRefundPayment.nm_Payment_Status__c='Payment made';
// RP.nm_Amount__c=opp.nm_AmountReceived__c;
objRefundPayment.Account__c=lstAppOpportuniyties[0].Accountid;
objRefundPayment.Mode_Of_Payment__c=ModeofPaymentSem1;
//objRefundPayment.Additional_Deduction__c=2000;
objRefundPayment.nm_Information_Center__c=lstAppOpportuniyties[0].nm_InformationCenters__c;
lstRefundpayment.add(objRefundPayment);
}
//commented for closed cases
if(lstAppOpportuniyties[0].StageName=='Closed'){
nmRefund_Payment__c objRefundPayment=new nmRefund_Payment__c();
objRefundPayment.Total_Refund_Amount__c=intTotalMountTorefund+intLatefees;
objRefundPayment.nm_Payment_Status__c='Payment made';
// RP.nm_Amount__c=opp.nm_AmountReceived__c;
objRefundPayment.Account__c=lstAppOpportuniyties[0].Accountid ;
objRefundPayment.Mode_Of_Payment__c=ModeofPaymentSem1;
//objRefundPayment.Additional_Deduction__c=0;
objRefundPayment.nm_Information_Center__c=lstAppOpportuniyties[0].nm_InformationCenters__c;
lstRefundpayment.add(objRefundPayment);
}
}
if(lstToUpdate.size()>0)
{
for(Opportunity objOppUpdat:lstToUpdate)
{
if(objOppUpdat.StageName=='Closed')
{
objOppUpdat.StageName='Registration Cancelled';
}
else
{
objOppUpdat.StageName='Admission Cancelled';
}
}
}
update lstToUpdate;
}
if(strEmailid !=null && strEmailid !=''){
list<lead> objLead=[select id from lead where Email=:strEmailid and isConverted=true];
if(objLead.size()>0)
{
try{
delete objLead;
}
catch(Exception e)
{
}
}
}
set<id> stid=new set<id>();
if(lstRefundpayment.size()>0)
{
insert lstRefundpayment;
for(nmRefund_Payment__c objRfPay:lstRefundpayment)
{
stid.add(objRfPay.id);
}
}
if(accountIds.size()>0){
CancelFutureHandler.CancelAdmission(accountIds,stid);
}
}
}
<------------------------------------------------------------------------------------------------------------------------------------------------------------------------->
Test Class :-
@istest(SeeAllData=true)
public class CancelAdmissionTracker {
Public static nm_Centers__c objCen;
Public static User objUser;
Public static nm_UserMapping__c objUserMap;
Public static nm_Program__c objProgram;
Public static nm_WorkExperience__c objWrkExp;
public static nmRefund_Payment__c objRefundPayment;
public static Account objAccount;
public static Opportunity opps;
static testMethod void testmethod1()
{
loadData();
objAccount.nm_StudentStatus__c='Registration Cancelled'; ------> (the status does not get updated post inserting.)
Test.StartTest();
update objAccount;
Test.StopTest();
}
static void loadData()
{
// List<Opportunity> lstOpps= new List<Opportunity>();
List<nm_EligiblityCriteria__c> lstElegCritria= new List<nm_EligiblityCriteria__c>();
List<Account> lstAcc1= new List<Account>();
List<Lead> lstLead = new List<Lead>();
nm_EligiblityCriteria__c objEC = new nm_EligiblityCriteria__c();
CommonTrackerClass.createEligibiltyCriteria(objEC);
lstElegCritria.add(objEC);
System.debug('**eligibility'+objEC);
nm_EligiblityCriteria__c objEC1 = new nm_EligiblityCriteria__c();
CommonTrackerClass.createEligibiltyCriteria(objEC1);
lstElegCritria.add(objEC1);
Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];
objUser = new User(Alias = 'newUser', Email='newuser@testorg.com',
EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Los_Angeles', UserName='newuseoijior@testorg.com');
insert objUser;
Recordtype RT=[select id from Recordtype where sobjectType='nm_Centers__c' AND name ='Information Center'];
objCen= new nm_Centers__c();
objCen.RecordTypeID = RT.id;
objCen.nm_CenterCode__c = '002';
objCen.nm_CenterCity__c = 'Delhi';
objCen.nm_City__c = 'Delhi';
objCen.nm_StateProvince__c = 'Delhi';
objCen.nm_Street__c = 'Krishna Nagar';
objCen.nm_PostalCode__c = '110051';
// objCen.Owner=objUser;
insert objCen;
objUserMap= new nm_UserMapping__c();
objUserMap.nm_Centers__c=objCen.id;
objUserMap.nm_User__c=objUser.id;
objUserMap.nm_Phone__c='326392156';
objUserMap.nm_Head__c= true;
insert objUserMap;
objAccount= new Account();
objAccount.nm_PreStudentNo__c ='77213';
objAccount.nm_StudentStatus__c='Confirmed';
objAccount.nm_SerialNo__c=2;
objAccount.nm_IsDocumentApproved__c = true;
objAccount.nm_Centers__c=objCen.id;
objAccount.lastname ='singh';
objAccount.FirstName='Chandra';
objAccount.nm_AccountEmail__c='chandra@gmail.com';
objAccount.lastname ='kumar';
objAccount.PersonMobilePhone='8377985721';
objAccount.PersonEmail='chandra@gmail.com';
objAccount.nm_EligiblityCriteria__c=objEC1.Id;
objAccount.nmCaptureOpportuntiyStage__c='Registration Done';
objAccount.ownerid = objUser.id;
insert objAccount;
}
}
Without knowing much about the lines that are / are not covered, I would start by making sure you're setting the following value in your unit test:
All Answers
Additionally, your test will not cover enough of you large trigger to reach the required 75% coverage. I would recommend that you read over this article [1] on testing strategies to work out how figure out what tests you need.
Lastly, I would highly recommend against using seeAllData=true unless you have a legitimate reason for using it. If you do not know 100% why you have it in your test, then you should not have it there.
NOTE: When adding code, please use the "Add a code sample" button (icon <>) to increase readability and make it easier to reference.
[1] http://blog.deadlypenguin.com/blog/testing/strategies/
Without knowing much about the lines that are / are not covered, I would start by making sure you're setting the following value in your unit test:
Thanks for the reply. I shall do as instructed and see if the code fires. il post the code again and comment at the places where it is isnt providing coverage.
Thanks.