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
rajesh_yrajesh_y 

test.starttest not working

Hi ,

In the following code i had written test coverage for this class but its not covered could you please suggest whats wrong in my code....
when i call some methods from test coverage those will not be invoked please help me  thanks in advance...
public class newpatient {
public string address1{set;get;}
public string address2{set;get;}
public string distance{set;get;}
public boolean selection{set;get;}
public string lastname{set;get;}
public id userid=userinfo.getuserid();
public string sample{set;get;}
 Patient__c patient=new Patient__c();
    public newpatient(ApexPages.StandardController controller)
     {
    patient = (Patient__c)controller.getRecord();
   //address2=patient.E_Address1__c+','+patient.City__c+','+patient.County__c; 
    selection=false;
    lastname='';
    selectall();
    }
    Navigator__c pat=new Navigator__c();
    public Navigator__c getpat()
    {
    return pat;
    }
    public void setPat(navigator__c pat)
    {
    pat=pat;
    }
   
    public patient__c getpatient()
    {
    return patient;
    }
    public Pagereference saverec()
    {
    string str=valueone+valuethree;
    if(str!=null)
    {
     //navigator__c navigator=[select id from navigator__c where fullname__c=:str and createdbyid=:userid limit 1];
     //patient.Assigned_Navigator__c=mapval.get(str);
    }
    //patient.distance__c=valuetwo;
    
    insert patient;
    pagereference p=new pagereference('/'+patient.id);
    return p;
    }
     Navigator__c n=new Navigator__c();    
    public void change()
    {
    selection=true;
    if(patient.Assigned_Navigator__c!=null)
    n=[select id,E_Address_1__c,City__c,states__C from Navigator__c where id=:patient.Assigned_Navigator__c];
    address1=n.E_Address_1__c+','+n.City__c+','+n.states__C;
    address2=patient.E_Address1__c+','+patient.City__c+','+patient.states__c+','+patient.Zip__c; 
    system.debug('address1'+address1+'Address2'+address2);
    
    //selectall();
     
    
    }
   public class wraperclass
   {
   public id id{set;get;}
   public string address{set;get;}
   public string E_Address1{set;get;}
   public string City{set;get;}
   public string country{set;get;}
   public boolean check{set;get;}
   public string addr{set;get;}
   public string cancertype{set;get;}
   public string fname{set;get;}
   public string lname{set;get;}
  }
   list<wraperclass> navilist=new list<wraperclass>();
   public string address{set;get;} 
   list<string> slist=new list<string>();
   public void filtering()
   {
   If(navilist.size()>0)
      navilist.clear();
      if(mapval.size()>0)
      mapval.clear();
       string address22='';
       string address11=patient.E_Address1__c+','+patient.City__c+','+patient.states__c;
 if(pat.Type_of_Cancer__c!=null && pat.Gender__c!=null&&Lastname=='')
 {
 //cancer type and Gender
  for(Navigator__c nv:[select fullname__c,id,E_Navigator_Last_Name__c,name,E_Address_1__c,City__c,states__C,Type_of_Cancer__c from Navigator__c where  Gender__c=:pat.Gender__c and Type_of_Cancer__c=:pat.Type_of_Cancer__c and Assigned_Patients__c<=2 and createdbyid=:userid and Been_Through_Orientation__c=:true])
   {
  
   wraperclass w=new wraperclass();
   w.id=nv.id;
   w.E_Address1=nv.E_Address_1__c;
   w.City=nv.city__c;
   //w.country=nv.County__c;
   w.address=nv.name+';'+nv.E_Address_1__c+','+nv.city__c+','+nv.states__C+';'+nv.Type_of_Cancer__c+';'+nv.E_Navigator_Last_Name__c;
   address=w.address;
   slist.add(address);
   w.addr=nv.E_Address_1__c+','+nv.city__c+','+nv.states__C; 
   w.cancertype=nv.Type_of_Cancer__c;
   w.fname=nv.name;
   w.lname=nv.E_Navigator_Last_Name__c;
   address22=address22+nv.id+nv.E_Address_1__c+','+nv.city__c+','+nv.states__C+'|';
   navilist.add(w); 
   mapval.put(nv.fullname__c,nv.id);
   }
    
    coordinates(address11,address22);
 }
 else if(pat.Type_of_Cancer__c!=null && pat.Gender__c==null&&Lastname=='')
 {
 //only for type of cancer
  for(Navigator__c nv:[select fullname__c,id,E_Navigator_Last_Name__c,name,E_Address_1__c,City__c,states__C,Type_of_Cancer__c from Navigator__c where  Type_of_Cancer__c=:pat.Type_of_Cancer__c and Assigned_Patients__c<=2 and createdbyid=:userid and Been_Through_Orientation__c=:true])
   {
   wraperclass w=new wraperclass();
   w.id=nv.id;
   w.E_Address1=nv.E_Address_1__c;
   w.City=nv.city__c;
   //w.country=nv.County__c;
   w.address=nv.name+';'+nv.E_Address_1__c+','+nv.city__c+','+nv.states__C+';'+nv.Type_of_Cancer__c+';'+nv.E_Navigator_Last_Name__c;
   address=w.address;
   slist.add(address);
     w.addr=nv.E_Address_1__c+','+nv.city__c+','+nv.states__C; 
   w.cancertype=nv.Type_of_Cancer__c;
   w.fname=nv.name;
   w.lname=nv.E_Navigator_Last_Name__c;
   address22=address22+nv.id+nv.E_Address_1__c+','+nv.city__c+','+nv.states__C+'|';
   navilist.add(w); 
   mapval.put(nv.fullname__c,nv.id);
   }
   
    coordinates(address11,address22);
 }
 else if(pat.Type_of_Cancer__c!=null && pat.Gender__c==null &&Lastname!='')
{
//cancertype and lastname
 for(Navigator__c nv:[select fullname__c,id,E_Navigator_Last_Name__c,name,E_Address_1__c,City__c,states__C,Type_of_Cancer__c from Navigator__c where Last_name__C=:Lastname and Type_of_Cancer__c=:pat.Type_of_Cancer__c and Assigned_Patients__c<=2 and createdbyid=:userid and Been_Through_Orientation__c=:true])
   {
   wraperclass w=new wraperclass();
   w.id=nv.id;
   w.E_Address1=nv.E_Address_1__c;
   w.City=nv.city__c;
   //w.country=nv.County__c;
   w.address=nv.name+';'+nv.E_Address_1__c+','+nv.city__c+','+nv.states__C+';'+nv.Type_of_Cancer__c+';'+nv.E_Navigator_Last_Name__c;
   address=w.address;
   slist.add(address);
    w.addr=nv.E_Address_1__c+','+nv.city__c+','+nv.states__C; 
   w.cancertype=nv.Type_of_Cancer__c;
   w.fname=nv.name;
   w.lname=nv.E_Navigator_Last_Name__c;
   address22=address22+nv.id+nv.E_Address_1__c+','+nv.city__c+','+nv.states__C+'|';
   navilist.add(w); 
   mapval.put(nv.fullname__c,nv.id);
}
    coordinates(address11,address22);
}
 else if(pat.Type_of_Cancer__c==null && pat.Gender__c==null &&Lastname!='')
{
//only lastname 
 for(Navigator__c nv:[select fullname__c,id,E_Navigator_Last_Name__c,name,E_Address_1__c,City__c,states__C,Type_of_Cancer__c from Navigator__c where Last_name__C=:Lastname and Assigned_Patients__c<=2 and createdbyid=:userid and Been_Through_Orientation__c=:true])
   {
   wraperclass w=new wraperclass();
   w.id=nv.id;
   w.E_Address1=nv.E_Address_1__c;
   w.City=nv.city__c;
   //w.country=nv.County__c;
   w.address=nv.name+';'+nv.E_Address_1__c+','+nv.city__c+','+nv.states__C+';'+nv.Type_of_Cancer__c+';'+nv.E_Navigator_Last_Name__c;
   address=w.address;
   slist.add(address);
    w.addr=nv.E_Address_1__c+','+nv.city__c+','+nv.states__C; 
   w.cancertype=nv.Type_of_Cancer__c;
   w.fname=nv.name;
   w.lname=nv.E_Navigator_Last_Name__c;
   address22=address22+nv.id+nv.E_Address_1__c+','+nv.city__c+','+nv.states__C+'|';
   navilist.add(w); 
   mapval.put(nv.fullname__c,nv.id);
}
    coordinates(address11,address22);
}
 else if(pat.Type_of_Cancer__c==null && pat.Gender__c!=null &&Lastname!='')
{
//lastname and gender
 for(Navigator__c nv:[select fullname__c,id,E_Navigator_Last_Name__c,name,E_Address_1__c,City__c,states__C,Type_of_Cancer__c from Navigator__c where Last_name__C=:Lastname and Gender__c=:pat.Gender__c and Assigned_Patients__c<=2 and createdbyid=:userid and Been_Through_Orientation__c=:true])
   {
   wraperclass w=new wraperclass();
   w.id=nv.id;
   w.E_Address1=nv.E_Address_1__c;
   w.City=nv.city__c;
   //w.country=nv.County__c;
   w.address=nv.name+';'+nv.E_Address_1__c+','+nv.city__c+','+nv.states__C+';'+nv.Type_of_Cancer__c+';'+nv.E_Navigator_Last_Name__c;
   address=w.address;
   slist.add(address);
    w.addr=nv.E_Address_1__c+','+nv.city__c+','+nv.states__C; 
   w.cancertype=nv.Type_of_Cancer__c;
   w.fname=nv.name;
   w.lname=nv.E_Navigator_Last_Name__c;
   address22=address22+nv.id+nv.E_Address_1__c+','+nv.city__c+','+nv.states__C+'|';
   navilist.add(w);
   mapval.put(nv.fullname__c,nv.id); 
   }coordinates(address11,address22);
}
 else if(pat.Type_of_Cancer__c==null && pat.Gender__c!=null &&Lastname=='')
{
//only gender
 for(Navigator__c nv:[select fullname__c,id,E_Navigator_Last_Name__c,name,E_Address_1__c,City__c,states__C,Type_of_Cancer__c from Navigator__c where  Gender__c=:pat.Gender__c  and Assigned_Patients__c<=2 and createdbyid=:userid and Been_Through_Orientation__c=:true])
   {
   wraperclass w=new wraperclass();
   w.id=nv.id;
   w.E_Address1=nv.E_Address_1__c;
   w.City=nv.city__c;
   //w.country=nv.County__c;
   w.address=nv.name+';'+nv.E_Address_1__c+','+nv.city__c+','+nv.states__C+';'+nv.Type_of_Cancer__c+';'+nv.E_Navigator_Last_Name__c;
   address=w.address;
   slist.add(address);
    w.addr=nv.E_Address_1__c+','+nv.city__c+','+nv.states__C; 
   w.cancertype=nv.Type_of_Cancer__c;
   w.fname=nv.name;
   w.lname=nv.E_Navigator_Last_Name__c;
   address22=address22+nv.id+nv.E_Address_1__c+','+nv.city__c+','+nv.states__C+'|';
   navilist.add(w); 
   mapval.put(nv.fullname__c,nv.id);
}
coordinates(address11,address22);
}
 else if(pat.Type_of_Cancer__c!=null && pat.Gender__c!=null &&Lastname!='')
{
//gender and cancertype and lastname
 for(Navigator__c nv:[select fullname__c,id,E_Navigator_Last_Name__c,name,E_Address_1__c,City__c,states__C,Type_of_Cancer__c from Navigator__c where Last_name__C=:Lastname and Gender__c=:pat.Gender__c and Type_of_Cancer__c=:pat.Type_of_Cancer__c and Assigned_Patients__c<=2 and createdbyid=:userid and Been_Through_Orientation__c=:true])
   {
   wraperclass w=new wraperclass();
   w.id=nv.id;
   w.E_Address1=nv.E_Address_1__c;
   w.City=nv.city__c;
   //w.country=nv.County__c;
   w.address=nv.name+';'+nv.E_Address_1__c+','+nv.city__c+','+nv.states__C+';'+nv.Type_of_Cancer__c+';'+nv.E_Navigator_Last_Name__c;
   address=w.address;
   slist.add(address);
    w.addr=nv.E_Address_1__c+','+nv.city__c+','+nv.states__C; 
   w.cancertype=nv.Type_of_Cancer__c;
   w.fname=nv.name;
   w.lname=nv.E_Navigator_Last_Name__c;
   address22=address22+nv.id+nv.E_Address_1__c+','+nv.city__c+','+nv.states__C+'|';
   navilist.add(w); 
   mapval.put(nv.fullname__c,nv.id);
}
coordinates(address11,address22);
}
   }
   map<string,id> mapval=new map<string,id>();
   public void selectall()
   {
   If(navilist.size()>0)
   navilist.clear();
   string address22='';
    for(Navigator__c nv:[select fullname__c,id,name,E_Navigator_Last_Name__c ,E_Address_1__c,City__c,states__C,Type_of_Cancer__c from Navigator__c where Type_of_Cancer__c=:patient.Cancer_Type__c and Assigned_Patients__c<=2 and createdbyid=:userid and Been_Through_Orientation__c=:true])
   {
   wraperclass w=new wraperclass();
   w.id=nv.id;
   w.E_Address1=nv.E_Address_1__c;
   w.City=nv.city__c;
   //w.country=nv.County__c;
   w.address=nv.name+';'+nv.E_Address_1__c+','+nv.city__c+','+nv.states__C+';'+nv.Type_of_Cancer__c+';'+nv.E_Navigator_Last_Name__c;
   address=w.address;
   slist.add(address);
   w.addr=nv.E_Address_1__c+','+nv.city__c+','+nv.states__C; 
   w.cancertype=nv.Type_of_Cancer__c;
   w.fname=nv.name;
   w.lname=nv.E_Navigator_Last_Name__c;
   address22=address22+nv.id+nv.E_Address_1__c+','+nv.city__c+','+nv.states__C+'|';
   navilist.add(w); 
   mapval.put(nv.fullname__c,nv.id);
     
   }
    string address11=patient.E_Address1__c+','+patient.City__c+','+patient.states__c; 
    coordinates(address11,address22);
   
  
}
 public String valueOne{get; set;}
 public String valuetwo{get; set;}
 public string valuethree{get;set;}
public void finding()
{
 valueOne =Apexpages.currentPage().getParameters().get('one');
  valuetwo =Apexpages.currentPage().getParameters().get('two');
   valuethree =Apexpages.currentPage().getParameters().get('three');
system.debug('hhhhhhhhhhhhhhhhhh'+valueone);
selection=false;
}
   
  public list<wraperclass> getnavilist()
   {
   return navilist;
   } 
      public static boolean isApexTest = false;
 public void Coordinates(string add1 ,string add2)
{
  string url='http://maps.googleapis.com/maps/api/distancematrix/xml?origins='+add1+'&destinations='+add2+'&mode=driving&language=en&sensor=false';
   //string url='http://maps.googleapis.com/maps/api/distancematrix/xml?origins=hyderabad&destinations=banglore|kurnool|delhi&mode=driving&language=en&sensor=false';
    Http h = new Http();
    HttpRequest req = new HttpRequest();
req.setHeader('Content-type', 'application/x-www-form-urlencoded');
req.setHeader('Content-length', '0');
req.setEndpoint(url);
req.setMethod('POST');
/*HttpResponse res = h.send(req);
String responseBody = res.getBody();
string[] Coordinates = responseBody.split(',',0);
  XmlStreamReader reader = res.getXmlStreamReader();*/
   XmlStreamReader reader;
            if(!isApexTest)
              { 
                 HttpResponse res = h.send(req);
                 String responseBody = res.getBody();
                 reader= res.getXmlStreamReader();
                 
               }
               else
               {
                string body='<text>4455</text>';
                reader=new XmlStreamReader(body);
                
               }
  readXMLResponse(reader); 
 
} 
public String mainForTest(){
        isApexTest = true;
        return '';
    }
   public String readXMLResponse(XmlStreamReader reader)
  {
     wlist=new list<navclass>();
     string retValue; // Read through the XML
     integer i=0;
     while(reader.hasNext())
    {
  
    if (reader.getEventType() == XmlTag.START_ELEMENT)
    {
      
      
      if (reader.getLocalName()=='text') 
      {
      
          reader.next();
        if (reader.getEventType()==XmlTag.characters)
       {
          retValue= reader.getText();
          system.debug('*********'+retvalue);
            navclass w=new navclass();
            //w.distanceval=1;
          if(retvalue.substring(retvalue.length()-2,retvalue.length())=='km')
          {
           sample=string.valueof(retvalue);
         
           retValue=retValue.replace(',','');
           integer value=integer.valueof(retValue.substring(0,retValue.length()-3)); 
           w.distanceval=value*0.62; 
            
           w.address=navilist.get(i).addr;
           w.cancertype=navilist.get(i).cancertype;
           w.firstname=navilist.get(i).fname;
           w.lastname=navilist.get(i).lname;
           w.id=navilist.get(i).id;
           wlist.add(w);
           system.debug('+++++++'+retvalue);
           i++;
        }
       }
       }
   
     }
     reader.next();
      
 }
 sample=string.valueof(wlist);
 system.debug('************'+wlist);
 return null;
 }
 list<navclass> wlist=new list<navclass>();
 public list<navclass> getwlist()
 {
 return wlist;
 }
 public void selectedone()
 {
 integer i=0;
 for(navclass n:wlist)
 {
 if(n.isSelected==true)
 {
 Navigator__c nv=[select id,E_Navigator_Last_Name__c from Navigator__c where id=:n.id];
 patient.Assigned_Navigator__c=nv.id;
 patient.Navigator_Last_Name__c=nv.E_Navigator_Last_Name__c;
 patient.Distance__c=string.valueof(n.distanceval);
 break;
 }
 
 i++;
 
 }
 wlist.remove(i);
 
 }
public class navclass
{
public string timeval{set;get;}
public decimal distanceval{set;get;}
public string address{set;get;}
public string lastName{set;get;}
public string firstname{set;get;}
public string cancertype{set;get;}
public id id{set;get;}
public boolean isSelected{set;get;}
}
  /*  
public static testMethod void mytest()
{
navigator__c n=new navigator__c(name='aaa',states__C='lowa',Date_of_Entry__c=system.today(),E_Navigator_Last_Name__c='lab',Zip_Code__c='5688876552',Ethnic_Racial__c='Asian',Date_of_Birth__c=system.today(),Last_name__c='l',Type_of_Cancer__c='**bleep** Cancer',Gender__C='male',Been_Through_Orientation__c=true);

insert n;
patient__c p=new patient__c (E_Electronic_Chart_ID__c='aa1',E_LastName__c='abc',Cancer_Type__c='**bleep** Cancer');
//insert p;
ApexPages.StandardController sc = new ApexPages.standardController(p);
newpatient np=new newpatient(sc); 
String retVal =np.mainForTest();

wraperclass w=new wraperclass();
test.starttest();

np.finding();
np.filtering();
test.stoptest();
}
public static testMethod void mytest2()
{
navigator__c n=new navigator__c(name='aaa',states__C='lowa',Date_of_Entry__c=system.today(),E_Navigator_Last_Name__c='lab',Zip_Code__c='5688876552',Ethnic_Racial__c='Asian',Date_of_Birth__c=system.today(),Last_name__c='l',Type_of_Cancer__c='**bleep** Cancer',Gender__C='male',Been_Through_Orientation__c=true);

insert n;
patient__c p=new patient__c ();
//insert p;
ApexPages.StandardController sc = new ApexPages.standardController(p);
newpatient np=new newpatient(sc); 
String retVal =np.mainForTest();

wraperclass w=new wraperclass();
System.test.starttest();

//np.mainForTest();
np.filtering();
system.test.stoptest();
}*/

private static testMethod void mytest()
{
      navigator__c n=new navigator__c(name='aaa',states__C='lowa',Date_of_Entry__c=system.today(),E_Navigator_Last_Name__c='lab',Zip_Code__c='5688876552',Ethnic_Racial__c='Asian',Date_of_Birth__c=system.today(),Last_name__c='l',Type_of_Cancer__c='**bleep** Cancer',Gender__C='male',Been_Through_Orientation__c=true);
      insert n;
      patient__c p=new patient__c (E_Electronic_Chart_ID__c='aa1',E_LastName__c='abc');

      ApexPages.StandardController sc = new ApexPages.standardController(p);
      newpatient np=new newpatient(sc); 
      
      newpatient np1=new newpatient (sc); 
      newpatient np2=new newpatient (sc);
      newpatient np3=new newpatient (sc);
      newpatient np4=new newpatient (sc);
      newpatient np5=new newpatient (sc);
      wraperclass w=new wraperclass();

    w.id=n.id;
    w.E_Address1=n.E_Address_1__c;
    w.City=n.city__c;
   // w.country=n.County__c;
    w.address=n.name+';'+n.E_Address_1__c+','+n.city__c+','+n.states__C+';'+n.Type_of_Cancer__c;
    np.address=w.address;
    np.navilist.add(w);
    np.lastname='l';
    np.valueOne='aaa';
    np.patient.Cancer_Type__c='**bleep** Cancer';
        PageReference pageRef = Page.newpatient; 
        Test.setCurrentPage(pageRef); 
        ApexPages.currentPage().getParameters().put('one','aaa'); 
    np.patient.E_Electronic_Chart_ID__c='aab';
    np.patient.E_LastName__c='aac';
    np.pat.Type_of_Cancer__c='**bleep** Cancer';
    np1.pat.Type_of_Cancer__c='**bleep** Cancer';
    np1.lastname='l';
    np1.pat.Gender__C='male';
       np2.pat.Gender__C='male';
       np3.lastname='l';
       np4.pat.Gender__C='male';
       np4.lastname='l';
       np5.pat.Type_of_Cancer__c='**bleep** Cancer';
       test.starttest();
     
     np.getpatient();
     
     np.change();
     np.getnavilist();
     np.selectall();
     np.finding();
     np.filtering();
     np1.filtering();
     np2.filtering();
     np3.filtering();
     np4.filtering();
     np5.filtering();
     np.saverec();
     np.setpat(n);
     np.getpat();
     test.stoptest();
}

 

}
Navatar_DbSupNavatar_DbSup

Hi,


Can you please check that do you have any class with name Test??? If yes then rename that to test1 or something else.

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

rajesh_yrajesh_y

Hi Thank you for your quick response,

 

I do not have any classes with name test in my org .

test.starttest is working fine for another classess but its not working for this classes

Rajesh SriramuluRajesh Sriramulu

Hi

 

U didn't create instance for wrapper class in test method  try to create it like this.

 

newpatient.wraperclass nw1 = new newpatient.wraperclass();

 

newpatient.navclass nnw = new newpatient.navclass nnw();

 

And for Test.Starttest()  and Test.StopTest() these are Predifined Test methods which will use for best practices see:

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_test.htm

 

and Let me know how much code coverage currently.

 

 

Regards,

Rajesh.