function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
neeedhelpneeedhelp 

Writing test class for a particular code

Hello Everyone,

     how to write test class for the following code......Here is my piece of code

 

Public void CreateContact(contact acc,string PersonType){
system.debug('contactinformation'+acc);
system.debug('persontype'+PersonType);
list<contact> ExistingContactList=[select Id,Name,Email from contact where Email=:acc.Email and FirstName=:acc.FirstName and LastName=:acc.LastName limit 1];
if(!ExistingContactList.IsEmpty()){
PersonTypeContactId=ExistingContactList[0].Id;
PersonTypeContactName=ExistingContactList[0].Name;
//do nothing
}
else {
list<CnP_PaaS__Contact_Mail_ids__c> AlternateMailList=[select Id,CnP_PaaS__Contact_Mail_id__c,CnP_PaaS__Contact__c,CnP_PaaS__Contact__r.Name from CnP_PaaS__Contact_Mail_ids__c where CnP_PaaS__Contact_Mail_id__c=:acc.Email and CnP_PaaS__Contact__r.FirstName=:acc.Firstname and CnP_PaaS__Contact__r.LastName=:acc.Lastname limit 1];
if(!AlternateMailList.IsEmpty()){
PersonTypeContactId=AlternateMailList[0].CnP_PaaS__Contact__c;
PersonTypeContactName=AlternateMailList[0].CnP_PaaS__Contact__r.Name;
}

else{
list<Contact_information_fields__c> TempList;
// firoj
if(PersonType=='Registrant'){
TempList=[select Id,Name,Field_name__c,Order_number__c,Required__c,Visible__c,Default_value__c from Contact_information_fields__c where Event_name__c=:EventId.Id Order by Order_number__c];
}
if(PersonType=='Attendee'){
TempList=[select Id,Name,Field_name__c,Order_number__c,Visible__c,Required__c,Default_value__c from Contact_information_fields__c where Registration_level__c=:LevelName and Event_name__c=null Order by Order_number__c];
}

for(Contact_information_fields__c tm:TempList)
{
if(tm.Required__c==true && tm.Visible__c==false)
{
acc.put(tm.Field_name__c,tm.Default_value__c);

}

}
// end here
system.debug('contactinformation'+acc);

insert acc;


list<CnP_PaaS__XML_Setting__c> cnpsetting=[select CnP_PaaS__Accounts__c,CnP_PaaS__No_Account__c,CnP_PaaS__Account_lp__c from CnP_PaaS__XML_Setting__c];
if(cnpsetting.size()>0)
{
if(cnpsetting[0].CnP_PaaS__Accounts__c==true && cnpsetting[0].CnP_PaaS__No_Account__c==false)
{
account account= new account(Name=acc.FirstName+' '+acc.LastName);
insert account;
acc.AccountId=account.id;
}
if(cnpsetting[0].CnP_PaaS__Account_lp__c!=null)
{
acc.AccountId=cnpsetting[0].CnP_PaaS__Account_lp__c;
}

update acc;

}
PersonTypeContactId=acc.Id;
PersonTypeContactName=acc.FirstName+' '+acc.LastName;
}
}
if(PersonType=='Registrant'){
Event_registrant_session__c CreateRec=new Event_registrant_session__c();
CreateRec.Name=PersonTypeContactName;
CreateRec.ContactId__c=PersonTypeContactId;
CreateRec.EventId__c=EventId.Id;
CreateRec.status__c='Pending';
CreateRec.Coupon_code__c=CouponCode;
insert CreateRec;
RegistrantId=CreateRec.Id;
}

if(PersonType=='Attendee'){
Event_attendee_session__c CreateRec=new Event_attendee_session__c();
CreateRec.Name=PersonTypeContactName;
CreateRec.ContactId__c=PersonTypeContactId;
CreateRec.EventId__c=EventId.Id;
CreateRec.Registration_level__c=LevelName;
CreateRec.Registrant_session_Id__c=ApexPages.currentPage().getParameters().get('Registrant');
CreateRec.status__c='Pending';
insert CreateRec;
AttendeeId=CreateRec.Id;
}
}

 

 

part of My test class for this is

 

public static testMethod void testEventRegistration(){

EventRegistration eventreg = new EventRegistration();

eventreg.CreateContact(c,'Attendee');

eventreg.CreateContact(c,'Registrant');

}

Thanks

neeedhelpneeedhelp

modified my test code like this but its not yet covering the class....Here is my tesy code

 

public static testMethod void testEventRegistration(){

EventRegistration eventreg = new EventRegistration();

CnP_PaaS__Contact_Mail_ids__c mail = new CnP_PaaS__Contact_Mail_ids__c(CnP_PaaS__Contact_Mail_id__c='john@gmail.com',CnP_PaaS__Contact__c=c.id);
insert mail;

eventreg.CreateContact(c,'Attendee');

eventreg.CreateContact(c,'Registrant');

}

 

Help!!!!

HariDineshHariDinesh

Hi,

 

I would like you suggest one point here.

 

If your code is not completly covered means : The data you have provided is not enought to the test class, to test all the scenarios of the main class.

 

lets take an example to make you understand the scenario:

------------------------------------------------------------------------------------------

if(!ExistingContactList.IsEmpty())

 {
      PersonTypeContactId=ExistingContactList[0].Id;
      PersonTypeContactName=ExistingContactList[0].Name;
      //do nothing
  }
else

 {
     list<CnP_PaaS__Contact_Mail_ids__c> AlternateMailList=[select    Id,CnP_PaaS__Contact_Mail_id__c,CnP_PaaS__Contact__c,CnP_PaaS__Contact__r.Name from CnP_PaaS__Contact_Mail_ids__c where CnP_PaaS__Contact_Mail_id__c=:acc.Email and CnP_PaaS__Contact__r.FirstName=:acc.Firstname and CnP_PaaS__Contact__r.LastName=:acc.Lastname limit 1];

------------------------------------------------------------------------------------------------------------------------------------------

For example: In the above code snippet bold lines are not covered and normal lines are covered by your test method;

 

What it means is, the data you given in test method satisfies IF condition (if(!ExistingContactList.IsEmpty()))

So the controller does not goes to else part.

 

The solution is:

 

Write one more test method and make sure to give data such that data will not satisfies

IF condition (if(!ExistingContactList.IsEmpty())).

Now the second method will cover the else part of the code.

 

 

Try to understand the above logic and try to implement it, means write/create test data in test class such that it will cover each condition in Main class.

 

if you need help in understanding the above logic please feel free to post  your questions.

 

(Here any board member does not have Meta data of your custom object, other rit.

so any one will help you to how to write the test method. Hope you understood.)