+ Start a Discussion
dotnet developedotnet develope 

can anyone help me to write test method for this class?

hi all,

i am in critical situation, as per client requirement i have written a class in sandbox. its working fine.

when it come to test coverage in eclipse. it was not giving more then 15% .

i want to give the whole code but blog is not supporting more the 20000 characters.

a sample of that code is in below.

Code:
global class LeadtoContactConversionClass{
WebService static String leadconvert(string id) {

Lead l = new Lead();
Contact c= new Contact();
Contact c_update=new Contact();
Contact c_create =new Contact();
Account a= new Account();
Contact_Commitment__c cust_Commit = new Contact_Commitment__c();
String str,strid='',FName='',LName='',fiscalyear='';
//strid=id;
Integer commit_val=0,ival=0,count_c=0;
string csi_tags,comm_sub,commit_name,info,csi_tag,commsub,lead_commsub,l_csi;
boolean ser_email;
Integer len_l,len_c;
string email1='',email2='';
Integer imon=0,iyear=0; 
//string id
//string id='00QT0000004S0y7';
 //Date date1;
l=[Select Address_type_1__c, Address_Type_2__c, City, City_1__c, City_2__c,Communications_Subscriptions__c, Company,Country, Country_1__c, Country_2__c, CreatedDate,CSI_Events_email_list__c, CSI_Tag__c, Description,Commitment_Type__c,Email, Email_1__c, Email_2__c, Email_2_type__c, email_type__c,Commitment_Comments__c,Commitment_Description__c,Commitment_Manager__c,commitment_name__c,Commitment_Role__c,Start_Date__c,End_Date__c, Event_2__c, Fax,Fax_2__c, FirstName, Forces_For_Good__c, HasOptedOutOfEmail, Id, Industry,Information_request__c, LastName, LeadSource, MobilePhone, Name, Phone,Phone_2__c, Phone_2_type__c, phone_type__c, PostalCode, Preferred_Address__c, RecordTypeId,Salutation, SER_email_list__c,State, State_2__c, State_Province_1__c, Status,Stage__c,Street, Street_Address__c, Street_Address_1__c, Street_Address_2__c, SUID__c, SystemModstamp, Title, Website,Zip_Code_2__c, Zip_Postal_Code_1__c from Lead where Id =:id];
//c=[Select Name,FirstName,LastName from Contact where Name=:l.name and (( Account.Name=:l.Company) or (Email=:l.Email or Email_2__c=:l.Email_2__c) or (Email=:l.Email_2__c or Email_2__c=:l.Email) )];
//l.adderror(c.name);
//count_c=[Select Count() from Contact where Name=:l.name and (( Account.Name=:l.Company) or(Email=:l.Email or Email_2__c=:l.Email_2__c) or (Email=:l.Email_2__c or Email_2__c=:l.Email) )];
info=l.Information_request__c;
csi_tags=l.CSI_Tag__c;
comm_sub=l.Communications_Subscriptions__c;
commit_name=l.commitment_name__c;
 //str=''+count_c;
date date1 = l.Start_Date__c;
//date date1 = Date.newInstance(l.Start_Date__c.year(),l.Start_Date__c.month(),l.Start_Date__c.day());//

s
if(commit_name!=null)
{
 
 //date date1 = l.Start_Date__c;
 imon =date1.month();             ---------- error is this line is not covered.
 iyear =date1.year();       ---------- error is this line is not covered.


}

 if(imon>8)  ---------- error is this line is not covered.

 {
  fiscalyear=''+(iyear+1); ---------- error is this line is not covered.

 
 }
 else
 {
  fiscalyear=''+iyear;
 }

 string l_commsub='',csi_events='',commsub_str;
 commsub_str=l.Communications_Subscriptions__c;
 if(l.LeadSource=='Online Registration')    ---------- error is this line is not covered.
 {
  if(commsub_str!=null)
  {
    l_commsub = l.Communications_Subscriptions__c+';CSI Newsletter';---------- error is this line is not covered.
  }
  else
  {
    l_commsub = 'CSI Newsletter'; 
  }
   csi_events='checked';
 } 
 else 
 {
   l_commsub = l.Communications_Subscriptions__c; ---------- error is this line is not covered.
   if(l.CSI_Events_email_list__c==true)
     {
    csi_events='checked';
   }

 }
}
...................
................... 



static testMethod void myTest()
 {
Integer commit_val=0,ival=0,count_c=0;
string csi_tags,comm_sub,commit_name,info,csi_tag,commsub,lead_commsub,l_csi;
boolean ser_email;
Integer len_l,len_c;
string email1='',email2='';
Integer imon=0,iyear=0; 

  Lead l1=new Lead();
  l1=[Select Id,FirstName,LastName from Lead where LastName=:'Test1'];
                imon=9;
  iyear=2008;
  leadsource='Online Registration';
  commsub_str='CSI Newsletter';
  l_commsub='CSI Newsletter';
  status='Open - Contacted';
  csinewslettertt=';CSI Newsletter';
  


  leadconvert(l1.Id); }
 
}  

in this class the test method is myTest(). the record of 'Test1' have all the field have full data.

for any  more clarification. my emaild 'chilakamahesh@gmail.com'.

 

thanks & regards,

Mahesh Chilaka.


 
jrotensteinjrotenstein
You have lots of IF and ELSE statements. Are you running tests that cause those code blocks to execute?
dotnet developedotnet develope

Hi John,

       Yes, i want to excute IF and ELSE blocks. could u help me to solve this issue.

       The following is the test method which i have written to test the above apex script.

      i am getting 17% test coverage.

     please suggest me the changes required in the following test method to get 100% test coverage.

     static testMethod void myTest()
{
Integer commit_val=0,ival=0,count_c=0;
string csi_tags,comm_sub,commit_name,info,csi_tag,commsub,lead_commsub,l_csi;
boolean ser_email;
Integer len_l,len_c;
string email1='',email2='';
Integer imon=0,iyear=0;

  Lead l1=new Lead();
  l1=[Select Id,FirstName,LastName from Lead where LastName=:'Test1'];
                imon=9;
  iyear=2008;
  leadsource='Online Registration';
  commsub_str='CSI Newsletter';
  l_commsub='CSI Newsletter';
  status='Open - Contacted';
  csinewslettertt=';CSI Newsletter';
 


  leadconvert(l1.Id); }



Thanks in advance.

Regards

Mahesh Chilaka.(i will be available at chilakamahesh@gmail.com) .

 

jrotensteinjrotenstein
Create a test for every possible situation, so that every IF and ELSE statement is executed. You can use the same test function, but create multiple objects, each of which tests a different part of your logic.

I don't know what your code is doing, but in general it would be something like:

Code:
static testMethod void TestLeadConvert() {

// Retrieve and Convert a lead
Lead lead = [Select Id,FirstName,LastName from Lead where LastName=:'Test1'];
leadconvert(lead[0].Id);

// Test that a Contact was created
Contact contact = [Select Id, LeadSource__c from Contact where <?? How test ??>];
System.assertEquals('Online Registration', c.LeadSource__c);
}

Have a look at the Force.com Cookbook and at other Testing-related messages on this forum to see more examples.



Message Edited by jrotenstein on 05-30-2008 10:05 AM
dotnet developedotnet develope

Thanks John

I will try with your suggessions .

 

Thanks & Regards

Mahesh Chilaka :smileyvery-happy: