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
Sheeba Narayanan 7Sheeba Narayanan 7 

i need your help to write a test class for this




How to write testmethod for getDetails() here.
public class addressDetails{

        public String latitude {get;set;}
        public String longitude {get;set;}
        ........
    }
     public addressDetails getDetails(String strAddress)
     {
             Http http = new Http();
            HttpRequest request = new HttpRequest();
         IF (strAddress!=null){
             strAddress=strAddress.replaceAll(' ', '+');
                    addressDetails addr=new addressDetails();
        if (latitude!='') { addr.latitude=latitude;}
        if (longitude!=''){ addr.longitude=longitude;}
        ........................
        System.debug('Value of adddress is here : ' +addr);
        
        return addr;
     }
Best Answer chosen by Sheeba Narayanan 7
Jay GuruswamyJay Guruswamy
Hi Sheeba, In the Apex Class,(Not testmethod), before making the call out, write something like this. if(!Test.isRunningTest()) HTTPResponse res= http.send(req); } that way, the test method will not go into this if. Best Regards, Jay

All Answers

Jay GuruswamyJay Guruswamy
Hi Sheeba,

I saved your code as below.

public class addressDetails{
    public String latitude {get;set;}
    public String longitude {get;set;}

    public addressDetails getDetails(String strAddress)
    {
        addressDetails addr=new addressDetails();
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        if(strAddress!=null){
            strAddress=strAddress.replaceAll(' ', '+');
            if (latitude!='') { 
                addr.latitude=latitude;
            }
            if (longitude!=''){ 
                addr.longitude=longitude;
            }
            System.debug('Value of adddress is here : ' +addr);
         }
     return addr;
    }
 }

The test class as below

@isTest
public with sharing class addressDetails_TestMethod{
    static testMethod void addressDetails_TestMethod() {
        Test.startTest();
            addressDetails mAD=new addressDetails();
            mAD.latitude='13.082680';
            mAD.longitude='80.270721';
            mAD.getDetails('100,YorkHill Drive, Annandale, VA, USA') ;
        Test.stopTest();
    }
}




It gives 100% coverage.

Best Regards
Jay
 
Sheeba Narayanan 7Sheeba Narayanan 7
Hi Jay ,
Thanks much for your reply. Here am getting this error while running test "Methods defined as TestMethod do not support Web service callouts" .
This line is making the error mAD.getDetails('100,YorkHill Drive, Annandale, VA, USA') ; 

FYI am coping my class again. 

public class AutocompleteAddressForm {
 
 Public String latitude{get;set;}
 Public String longitude{get;set;}
    
    public class addressDetails{

        public String latitude {get;set;}
        public String longitude {get;set;}
        
    }
  
     public addressDetails getDetails(String strAddress)
     {
        Http http = new Http();
        HttpRequest request = new HttpRequest();
         IF (strAddress!=null){
             strAddress=strAddress.replaceAll(' ', '+');
         
    addressDetails addr=new addressDetails();
        if (latitude!='') { addr.latitude=latitude;}
        if (longitude!=''){ addr.longitude=longitude;}
        
        System.debug('Value of adddress is here : ' +addr);
        
        return addr;
         
         
     }}
    
    } 


Thanks
Sheeba
Jay GuruswamyJay Guruswamy
Hi Sheeba, In the Apex Class,(Not testmethod), before making the call out, write something like this. if(!Test.isRunningTest()) HTTPResponse res= http.send(req); } that way, the test method will not go into this if. Best Regards, Jay
This was selected as the best answer
Sheeba Narayanan 7Sheeba Narayanan 7
Thanks Jay. It works!!