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
raji Gallaraji Galla 

Test Class for Remote action and custom controller

Hi All,
 
can any one please tell me how to write test code for  remoting method in Apex class.And I am passing argument from page.
Please find my code:
i getting error System.Null point Exception at remote method calling in testclass.

User-added image
Visualforce
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
    <meta charset="utf-8"/>
    <title>Geocoding service</title>
    <style>
      html, body, #map-canvas {
        height: 100%;
        margin: 0px;
        padding: 0px
      }
      #panel {
        position: absolute;
        top: 5px;
        left: 50%;
        margin-left: -180px;
        z-index: 5;
        background-color: #fff;
        padding: 5px;
        border: 1px solid #999;
      }
    </style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
    <script>
        var geocoder;
        var map;
        //var latitude = place.geometry.location.lat();
        function initialize() {
        //alert (latitude);
          geocoder = new google.maps.Geocoder();
          var latlng = new google.maps.LatLng(37.4419, -122.1419);
          var mapOptions = {
            zoom: 15,
            center: latlng
          }
          map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
          codeAddress();
        }
        
        function codeAddress() {         
          var address = "{!Account.BillingStreet}, " + "{!Account.BillingCity}, " + "{!Account.BillingPostalCode}, " + "{!Account.BillingCountry}";
          alert(address);
          var infowindow = new google.maps.InfoWindow();
          geocoder.geocode( { 'address': address}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                map.setCenter(results[0].geometry.location);
                var marker = new google.maps.Marker({
                  map: map,
                  position: results[0].geometry.location
                });
                
                google.maps.event.addListener(marker, 'click', function() {
                    infowindow.setContent(address);
                    infowindow.open(map, marker);
                });
                google.maps.event.trigger(marker, "click");
                alert(results[0].geometry.location.lat());
                if (navigator.geolocation) {
                 navigator.geolocation.getCurrentPosition(function(position){
                     lat = position.coords.latitude;
                     lon = position.coords.longitude; 
                     alert('lat =' +lat);
                     var accntLat = results[0].geometry.location.lat();
                     var accntLong = results[0].geometry.location.lng();
                     alert("accntLat11 =" +accntLat);
                     alert("accntLong11 =" +accntLong);
                     // Use Visualforce JavaScript Remoting to query for nearby warehouses      
                     Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.AutoCheckInController.autoCheckIn}', lat, lon, accntLat, accntLong, "{!Account.Name}",
                         function(result, event){
                             if (event.status) {
                                 console.log(result);
                                 //createMap(lat, lon, result);           
                             } else if (event.type === 'exception') {
                                 //exception case code          
                             } else {
                                            
                             }
                          }, 
                          {escape: true}
                      );
                  });
                }
            } else {
              alert('Geocode was not successful for the following reason: ' + status);
            }
          });
        }
        
        google.maps.event.addDomListener(window, 'load', initialize);
        
    </script>
  </head>

  <body>
    <div id="map-canvas" style="width: 1000px; height: 300px"></div>
  </body>
</html>
Class===================
 public static Account recAccount;
    public string Latitude{get; set;}
    
    public AutoCheckInController (ApexPages.StandardController controller) { 
        //recAccount = (Account)Controller.getRecord();
    }

    @RemoteAction
    // Auto checkin the user if the current user's location is in and around 0.5 kms radius of Account/Lead/Retailer address
    global static User_Tracking__c autoCheckIn(Double lat, Double lon, Double accntLat, Double accntLng, String strAccntName) {
              
        System.debug('User Latitude =' +lat);
        System.debug('User Longitude =' +lon);
        
        System.debug('Account Name=' + strAccntName);
        System.debug('Account Latitude =' +accntLat);
        System.debug('Account Longitude =' +accntLng);
        
        // convert to radians
        Double dDepartLat = lat * 3.14159 / 180;
        Double dDepartLong = lon * 3.14159 / 180;
        Double dArrivalLat = accntLat * 3.14159 / 180;
        Double dArrivalLong = accntLng * 3.14159 / 180;
        
        Double dDeltaLong = dArrivalLong - dDepartLong;
        Double dDeltaLat = dArrivalLat - dDepartLat;
        
        // calculate angle using the haversine formula
        Double dHaversineResult = Math.Sin( dDeltaLat / 2 ) * Math.Sin( dDeltaLat / 2 ) 
                                  + Math.Cos( dDepartLat ) * Math.Cos( dArrivalLat ) 
                                    * Math.Sin( dDeltaLong / 2 ) * Math.Sin( dDeltaLong / 2 );
        
        // calculate distance by multiplying arc-tangent by the planet radius in miles
        Double dDistance = 1.60934 * 3958.76 * 2 * Math.Atan2( Math.Sqrt( dHaversineResult ), Math.Sqrt( 1 - dHaversineResult ) );
        
        System.debug('Distance = ' + dDistance);
        
        GeoTagging_Distance__c gtd = GeoTagging_Distance__c.getValues('Geo Tagging');
        
        System.debug('Configured Distance = ' +gtd.Distance__c);
        
        If(dDistance <= gtd.Distance__c)
        {
            
            String strDate = String.valueOf(DateTime.Now());
            System.debug('strDate=' +strDate);
            
            User_Tracking__c recTrack = New User_Tracking__c();
            
            recTrack.User__c = Userinfo.getUserId();
            recTrack.Location__Latitude__s = Decimal.ValueOf(lat);
            recTrack.Location__Longitude__s = Decimal.ValueOf(lon);
            recTrack.Visited_Date__c = strDate;
            recTrack.Account_Name__c = strAccntName;
            //recTrack.Location_Date__c = System.Today();
            try
            {
                Insert recTrack;
                system.debug('@@@'+recTrack);
            }
            Catch(Exception e)
            {
                System.debug('Exception Raised ' +e.getMessage());
            }
        }
        return Null;
    }
}
My test class====================
@isTest
Public class TestClassforAutoCheckInController{

public static testMethod void testMyController() {

//our logic

Account acc = new Account();
    Double lat = 55.04;
    Double lon = 67.98;
    Double accntLat = 67.98;
    Double accntLng  =77.98 ;    
    acc.name = 'test';   
    insert acc;
  account  alist = [select id, name from account where name =: 'test'];
    String strAccntName = alist.id;
     AutoCheckInController.autoCheckIn(lat ,lon ,accntLat,accntLng , strAccntName );
         
    // convert to radians
        Double dDepartLat = lat * 3.14159 / 180;
        Double dDepartLong = lon * 3.14159 / 180;
        Double dArrivalLat = accntLat * 3.14159 / 180;
        Double dArrivalLong = accntLng * 3.14159 / 180;
        Double dDeltaLong = dArrivalLong - dDepartLong;
        Double dDeltaLat = dArrivalLat - dDepartLat;
        // calculate angle using the haversine formula
        Double dHaversineResult = Math.Sin( dDeltaLat / 2 ) * Math.Sin( dDeltaLat / 2 ) 
                                  + Math.Cos( dDepartLat ) * Math.Cos( dArrivalLat ) 
                                    * Math.Sin( dDeltaLong / 2 ) * Math.Sin( dDeltaLong / 2 );
                                    
   // calculate distance by multiplying arc-tangent by the planet radius in miles
        Double dDistance = 1.60934 * 3958.76 * 2 * Math.Atan2( Math.Sqrt( dHaversineResult ), Math.Sqrt( 1 - dHaversineResult ) );
         GeoTagging_Distance__c gtd = GeoTagging_Distance__c.getValues('Geo Tagging');
         If(dDistance <= gtd.Distance__c)
        {
            
            String strDate = String.valueOf(DateTime.Now());
            System.debug('strDate=' +strDate);
            
            User_Tracking__c recTrack = New User_Tracking__c();
            
            recTrack.User__c = Userinfo.getUserId();
            recTrack.Location__Latitude__s = Decimal.ValueOf(lat);
            recTrack.Location__Longitude__s = Decimal.ValueOf(lon);
            date  d = system.today();
             recTrack.Visited_Date__c = string.valueof(d);
            recTrack.Account_Name__c = 'test';
           
            try
            {
                Insert recTrack;
                system.debug('@@@'+recTrack);
            }
            Catch(Exception e)
            {
                System.debug('Exception Raised ' +e.getMessage());
            }
        }
         

}
Thanks in advance,
Raji