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
sandeepathadusandeepathadu 

how to write test case for my controller

hi all

 

 

I have written a controller for inserting bulk appointments. Actually the objective is i have created an appointment object in which i have two fields FROM and TO. I have taken these fields into another visual force page.Now user goes to that visual force page and enters the from and to timings.For ex:user want bulk appoitments from 1 o clock to 3 then in from field he enters 1 and to field he enters 3. and he will get bulk appointments from 1:00,1:10 1:20 so on upto 2:50. for this i have written the following controller

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];
and now u see i have declared userinputappointment as a value that is taken from the user . now i have written the test case li8ke this for my controller
@isTest
private class extwoTestclass
{

static testmethod void TestController() 
{
Staff__c s1 = new Staff__c();
      s1.name='Rahul';
      s1.Mobile__c='9849098490';
      insert s1;

Branch__c b = new Branch__c();
b.Name = 'Test';
b.Region__c = 'Market Price';
insert b;

Account a = new Account ();
a.LastName = 'Test';
a.Branch__c = b.id;
a.PersonMobilePhone = '9550412970';
insert a ;

       Appointment__c app = new Appointment__c();
        app.from__c='6';
        app.account__c=a.id;
        app.from_min__c = '00';
        app.Appt_AM_PM__c='PM';
        app.Appt_Date__c=date.ValueOf('2011-05-21');
        app.Appt_Hour__c='9';
        app.Appt_Minute__c='80';
        app.RecordTypeid='012N0000000CgZ2';
        app.Branch__c=b.id;
        app.to_Min__c='40';
        app.to__c='7';       
        insert app;
 
  
  
  
ApexPages.currentPage().getParameters().put('id', app.id);
  

        ApexPages.StandardController con = new ApexPages.StandardController(app); 
       
            ExtTwo cs = new ExtTwo(con);    
        
        pagereference s7 = cs.save();
   
    } 
    
 
}
now i am getting this error system.null pointer expression.Class.ExtTwo.save: line 24, column 27 Class.extwoTestclass.TestController: line 60, column 28 External entry point
now this error indicatres in my controller the userinputappointment.from__c and all fields are taking null values plz help me in writing the test case for my code

 

 

Best Answer chosen by Admin (Salesforce Developers) 

All Answers

sandeepathadusandeepathadu

thanks again got 100% code coverage