+ Start a Discussion
dotnet developedotnet develope 

can anybody 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()
 {
  Lead l1=new Lead();
  l1=[Select Id,FirstName,LastName from Lead where LastName=:'Test1'];
  
  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.

DevAngelDevAngel
Hi dotnet_develope,

I think you need more cases. You will need to create test data that will ensure that in the cases where you are checking for null, you actually have null values. This is a pretty standard unit testing approach. You can't just test for one case, but all the cases that you are checking for null on as well.


Cheers
dotnet developedotnet develope

hi dave,

i have tried with multiple cases with different variation but still the test coverage is 16%.

static testMethod void myTest()

{

Lead l1=new Lead();

l1=[Select Id,FirstName,LastName from Lead where LastName=:'Test1'];

leadconvert(l1.id);

Lead l2=new Lead();

l2=[Select Id,FirstName,LastName from Lead where LastName=:'Test2'];

leadconvert(l2.id);

Lead l3=new Lead();

l3=[Select Id,FirstName,LastName from Lead where LastName=:'Test3'];

leadconvert(l3.id);

}

is there any other way to write the cases of this class.
 
Regards
Mahesh Chilaka