+ Start a Discussion
Admin DNAAdmin DNA 

get ip address test Class

Hi,
I'm trying to test a class wher I get current User Ip
@AuraEnabled    
public static String GetUserIPAddress() {
string ReturnValue = Auth.SessionManagement.getCurrentSession().get('SourceIp');

return ReturnValue;
} // GetUserIPAddress



Test class:

 public static testmethod void Test1()
 {System.runAs(new User(Id = UserInfo.getUserId())){
     
     String ip = siteLeadForm.GetUserIPAddress();}
       
        

    }

In sandbox it is ok, but in production I receive this error:
System.UnexpectedException: Current session unavailable 
Stack Trace: Class.Auth.SessionManagement.getCurrentSession: line 5, column 1 Class.siteLeadForm.GetUserIPAddress: line 18, column 1 Class.siteLeadFormTEST.Test1: line 17, column 1


Any Ideas?
thankyou in advance
Best Answer chosen by Admin DNA
karthikeyan perumalkarthikeyan perumal
Hello, 

Use this key word  in your test class  "if(!Test.isRunningTest())" 
 
if(!Test.isRunningTest())
   {
     System.runAs(new User(Id = UserInfo.getUserId())){
     
      String ip = siteLeadForm.GetUserIPAddress();
     
     }
   }


like above said,

hope this will help 

Thanks
karthik
 

All Answers

karthikeyan perumalkarthikeyan perumal
Hello, 

Use this key word  in your test class  "if(!Test.isRunningTest())" 
 
if(!Test.isRunningTest())
   {
     System.runAs(new User(Id = UserInfo.getUserId())){
     
      String ip = siteLeadForm.GetUserIPAddress();
     
     }
   }


like above said,

hope this will help 

Thanks
karthik
 
This was selected as the best answer
Admin DNAAdmin DNA
Hi Karthik, I've tryed but I have a problem of coverage now, It is only 70%
Admin DNAAdmin DNA
Ok Kathik in the production It runs. But for the Guest user profile I can't get the Ip. Any ideas?