+ Start a Discussion
sandeepathadusandeepathadu 

Hi how to write test case for the fields that are taking null values in the controller

Hi all

 

 

 

I have written a controller that have null fields that user has to enter.In this case wheen i write test  case for my controller i am gettin argument cannot be null.plz help me to write test case in this kind of situatins

Best Answer chosen by Admin (Salesforce Developers) 
My OwnMy Own

try this code. 

@isTest
private class TestPost {

    static testMethod void myUnitTest() {
    	
    	// Assign the Data for your object
    	Appointment__c objAppt=new Appointment__c(); 
    	objAppt.from__c='';
    	objAppt.To__c = '';
    	objAppt.doctor__c = ''; 
    	objAppt.To_Min__c =''; 
		// Assign all your Data to the Obj Appointment__c 
		
		insert objAppt; 
    	
        ApexPages.StandardController poStdCon = new ApexPages.StandardController(objAppt);   
        ExtTwo testPage = new  ExtTwo(poStdCon);
        testPage.userInputAppointment = objAppt;  
        testPage.save(); 
        
    }
}

 

All Answers

My OwnMy Own

Can you please post your code here. 

sandeepathadusandeepathadu
public class ExtTwo
{
public ExtTwo(ApexPages.StandardController controller) 
{ 
userInputAppointment = new Appointment__c();

}
public Appointment__c userInputAppointment {get; set;}
public list<appointment__c> apps {get;set;}  
public list<appointment__c> apps1 {get;set;}  
public list<appointment__c> apps2 {get;set;}
public list<appointment__c> apps3 {get;set;}
public list<appointment__c> apps4 {get;set;}
public list<appointment__c> apps5 {get;set;}

public PageReference save()
{
apps = new list<appointment__c>();
apps1 = new list<appointment__c>();
apps2 = new list<appointment__c>();
apps3 = new list<appointment__c>();
apps4 = new list<appointment__c>();
apps5 = new list<appointment__c>();
integer i=integer.valueof(userInputAppointment.from__c);          
integer i1=integer.valueof(userInputAppointment.To__c);
integer i2=integer.valueof(userInputAppointment.from_min__c);
integer i3=integer.valueof(userInputAppointment.To_Min__c);

if(i<i1)
{ 
for(integer k=0;k<(60-i2)/10;k++)
{
appointment__c ap = new appointment__c();
ap.branch__c =  userInputAppointment.branch__c;
ap.account__c = userInputAppointment.Account__c;
ap.doctor__c=userInputAppointment.doctor__c;
ap.appt_date__c = userInputAppointment.appt_date__c;
ap.Appt_AM_PM__c= 'AM';
if(integer.valueof(userInputAppointment.from__c)>=12)
{
ap.Appt_AM_PM__c= 'PM';
}
ap.Appt_Hour__c =String.valueOf(integer.valueof(userInputAppointment.from__c));
if(integer.valueof(userInputAppointment.from__c)>12)
{
ap.Appt_Hour__c =String.valueOf(integer.valueof(userInputAppointment.from__c)-12);
}
ap.Appt_Minute__c = userInputAppointment.from_min__c;
ap.RecordTypeid=userInputAppointment.RecordTypeid;
if(i2!=integer.valueof(userInputAppointment.from_min__c))
{
ap.RecordTypeid='012N0000000CgSu';
}
apps1.add(ap);
}
insert apps1[0];
}
return null;
}  
plz help me write the test case for my code

 

My OwnMy Own

try this code. 

@isTest
private class TestPost {

    static testMethod void myUnitTest() {
    	
    	// Assign the Data for your object
    	Appointment__c objAppt=new Appointment__c(); 
    	objAppt.from__c='';
    	objAppt.To__c = '';
    	objAppt.doctor__c = ''; 
    	objAppt.To_Min__c =''; 
		// Assign all your Data to the Obj Appointment__c 
		
		insert objAppt; 
    	
        ApexPages.StandardController poStdCon = new ApexPages.StandardController(objAppt);   
        ExtTwo testPage = new  ExtTwo(poStdCon);
        testPage.userInputAppointment = objAppt;  
        testPage.save(); 
        
    }
}

 

This was selected as the best answer
sandeepathadusandeepathadu

great great great thank u very much it works exactly but i got only 60 percent coverage any ways u did this for me thank u very much

My OwnMy Own

 

 

Try to set your conditional data to your objects and try to call that from your code.  

 

// Assign the Data for your object
    	Appointment__c objAppt1 = new Appointment__c(); 
    	objAppt1 .from__c=12;
    	objAppt1 .To__c = '';
    	objAppt1 .doctor__c = ''; 
    	objAppt1 .To_Min__c =''; 
                objAppt1 .Appt_AM_PM__c = 'PM'; 
		// Assign all your Data to the Obj Appointment__c 
		
		insert objAppt1 ; 
    	
        ApexPages.StandardController poStdCon = new ApexPages.StandardController(objAppt1);   
        ExtTwo testPage = new  ExtTwo(poStdCon);
        testPage.userInputAppointment = objAppt1;  
        testPage.save(); 

 

 

Try with this now.  If everything working fine then mark this completed.

 

Happy Coding. 

sandeepathadusandeepathadu

yeah actually i posted only half the code i have written .shall i post the full code

 

ursmahiursmahi

how doi write testcase for this please help me

Public PageReference osave() {
List<Order_Line_Items__c> olilist= new List<Order_Line_Items__c>();
ro.Retailers__c=retailer;
insert ro;
Set<id> scbf = new Set<id>();
Set<id> spn = new Set<id>();
if(oli1.Sr__c!=null && oli1.Quantity__c!=null)
{

oli1.Orders__c=ro.id;
pn1=oli1.Part_Number__c;
Part_Number__c ps=[select id,CBF__c,MRP_Rs__c from Part_Number__c where id=:pn1];
oli1.CBF__c=ps.CBF__c;
oli1.Part_Number__c=ps.id;
oli1.Value__c=ps.MRP_Rs__c*oli1.quantity__c;
scbf.add(ps.CBF__c);
spn.add(ps.id);
olilist.add(oli1);
}
if(oli2.Sr__c!=null && oli2.Quantity__c!=null)
{
oli2.Orders__c=ro.id;
pn2=oli2.Part_Number__c;
Part_Number__c ps2=[select id,CBF__c,MRP_Rs__c from Part_Number__c where id=:pn2];
oli2.CBF__c=ps2.CBF__c;
oli2.Part_Number__c=ps2.id;
oli2.Value__c=ps2.MRP_Rs__c*oli2.quantity__c;
scbf.add(ps2.CBF__c);
spn.add(ps2.id);
olilist.add(oli2);
}

if(oli3.Sr__c!=null && oli3.Quantity__c!=null)
{
oli3.Orders__c=ro.id;
pn3=oli3.Part_Number__c;
Part_Number__c ps3=[select id,CBF__c,MRP_Rs__c from Part_Number__c where id=:pn3];
oli3.CBF__c=ps3.CBF__c;
oli3.Part_Number__c=ps3.id;
oli3.Value__c=ps3.MRP_Rs__c*oli3.quantity__c;
scbf.add(ps3.CBF__c);
spn.add(ps3.id);
olilist.add(oli3);
}
if(oli4.Sr__c!=null && oli4.Quantity__c!=null)
{
oli4.Orders__c=ro.id;
pn4=oli4.Part_Number__c;
Part_Number__c ps4=[select id,CBF__c,MRP_Rs__c from Part_Number__c where id=:pn4];
oli4.CBF__c=ps4.CBF__c;
oli4.Part_Number__c=ps4.id;
oli4.Value__c=ps4.MRP_Rs__c*oli4.quantity__c;
scbf.add(ps4.CBF__c);
spn.add(ps4.id);
olilist.add(oli4);
}
if(oli5.Sr__c!=null && oli5.Quantity__c!=null)
{
oli5.Orders__c=ro.id;
pn5=oli5.Part_Number__c;
Part_Number__c ps5=[select id,CBF__c,MRP_Rs__c from Part_Number__c where id=:pn5];
oli5.CBF__c=ps5.CBF__c;
oli5.Part_Number__c=ps5.id;
oli5.Value__c=ps5.MRP_Rs__c*oli5.quantity__c;
scbf.add(ps5.CBF__c);
spn.add(ps5.id);
olilist.add(oli5);
}


if(oli6.Sr__c!=null && oli6.Quantity__c!=null)
{
oli6.Orders__c=ro.id;
pn6=oli6.Part_Number__c;
Part_Number__c ps6=[select id,CBF__c,MRP_Rs__c from Part_Number__c where id=:pn6];
oli6.CBF__c=ps6.CBF__c;
oli6.Part_Number__c=ps6.id;
oli6.Value__c=ps6.MRP_Rs__c*oli6.quantity__c;
scbf.add(ps6.CBF__c);
spn.add(ps6.id);
olilist.add(oli6);
}


if(oli7.Sr__c!=null && oli7.Quantity__c!=null)
{
oli7.Orders__c=ro.id;
pn7=oli7.Part_Number__c;
Part_Number__c ps7=[select id,CBF__c,MRP_Rs__c from Part_Number__c where id=:pn7];
oli7.CBF__c=ps7.CBF__c;
oli7.Part_Number__c=ps7.id;
oli7.Value__c=ps7.MRP_Rs__c*oli7.quantity__c;
scbf.add(ps7.CBF__c);
spn.add(ps7.id);
olilist.add(oli7);
}

if(oli8.Sr__c!=null && oli8.Quantity__c!=null)
{
oli8.Orders__c=ro.id;
pn8=oli8.Part_Number__c;
Part_Number__c ps8=[select id,CBF__c,MRP_Rs__c from Part_Number__c where id=:pn8];
oli8.CBF__c=ps8.CBF__c;
oli8.Part_Number__c=ps8.id;
oli8.Value__c=ps8.MRP_Rs__c*oli8.quantity__c;
scbf.add(ps8.CBF__c);
spn.add(ps8.id);
olilist.add(oli8);
}

if(oli9.Sr__c!=null && oli9.Quantity__c!=null)
{
oli9.Orders__c=ro.id;
pn9=oli9.Part_Number__c;
Part_Number__c ps9=[select id,CBF__c,MRP_Rs__c from Part_Number__c where id=:pn9];
oli9.CBF__c=ps9.CBF__c;
oli9.Part_Number__c=ps9.id;
oli9.Value__c=ps9.MRP_Rs__c*oli9.quantity__c;
scbf.add(ps9.CBF__c);
spn.add(ps9.id);
olilist.add(oli9);
}

if(oli10.Sr__c!=null && oli10.Quantity__c!=null)
{
oli10.Orders__c=ro.id;
pn10=oli10.Part_Number__c;
Part_Number__c ps10=[select id,CBF__c,MRP_Rs__c from Part_Number__c where id=:pn10];
oli10.CBF__c=ps10.CBF__c;
oli10.Part_Number__c=ps10.id;
oli10.Value__c=ps10.MRP_Rs__c*oli10.quantity__c;
scbf.add(ps10.CBF__c);
spn.add(ps10.id);
olilist.add(oli10);
}

if(oli11.Sr__c!=null && oli11.Quantity__c!=null)
{
oli11.Orders__c=ro.id;
pn11=oli11.Part_Number__c;
Part_Number__c ps11=[select id,CBF__c,MRP_Rs__c from Part_Number__c where id=:pn11];
oli11.CBF__c=ps11.CBF__c;
oli11.Part_Number__c=ps11.id;
oli11.Value__c=ps11.MRP_Rs__c*oli11.quantity__c;
scbf.add(ps11.CBF__c);
spn.add(ps11.id);
olilist.add(oli11);
}

if(oli12.Sr__c!=null && oli12.Quantity__c!=null)
{
oli12.Orders__c=ro.id;
pn12=oli12.Part_Number__c;
Part_Number__c ps12=[select id,CBF__c,MRP_Rs__c from Part_Number__c where id=:pn12];
oli12.CBF__c=ps12.CBF__c;
oli12.Part_Number__c=ps12.id;
oli12.Value__c=ps12.MRP_Rs__c*oli12.quantity__c;
scbf.add(ps12.CBF__c);
spn.add(ps12.id);
olilist.add(oli12);
}

if(oli13.Sr__c!=null && oli13.Quantity__c!=null)
{
oli13.Orders__c=ro.id;
pn13=oli13.Part_Number__c;
Part_Number__c ps13=[select id,CBF__c,MRP_Rs__c from Part_Number__c where id=:pn13];
oli13.CBF__c=ps13.CBF__c;
oli13.Part_Number__c=ps13.id;
oli13.Value__c=ps13.MRP_Rs__c*oli13.quantity__c;
scbf.add(ps13.CBF__c);
spn.add(ps13.id);
olilist.add(oli13);
}

if(oli14.Sr__c!=null && oli14.Quantity__c!=null)
{
oli14.Orders__c=ro.id;
pn14=oli14.Part_Number__c;
Part_Number__c ps14=[select id,CBF__c,MRP_Rs__c from Part_Number__c where id=:pn14];
oli14.CBF__c=ps14.CBF__c;
oli14.Part_Number__c=ps14.id;
oli14.Value__c=ps14.MRP_Rs__c*oli14.quantity__c;
scbf.add(ps14.CBF__c);
spn.add(ps14.id);
olilist.add(oli14);
}

if(oli15.Sr__c!=null && oli15.Quantity__c!=null)
{
oli15.Orders__c=ro.id;
pn15=oli15.Part_Number__c;
Part_Number__c ps15=[select id,CBF__c,MRP_Rs__c from Part_Number__c where id=:pn15];
oli15.CBF__c=ps15.CBF__c;
oli15.Part_Number__c=ps15.id;
oli15.Value__c=ps15.MRP_Rs__c*oli15.quantity__c;
scbf.add(ps15.CBF__c);
spn.add(ps15.id);
olilist.add(oli15);
}

 

insert olilist;
ro.Count_of_CBFs__c=scbf.size();
ro.Count_of_P_Nos__c=spn.size();
update ro;

PageReference opptyPage = new PageReference('/apex/PartnerPortalSecondarySale');
opptyPage.setRedirect(true);
return opptyPage;
}