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
EchoEchoEchoEcho 

Site.getName() always null in test method

Is it possible to "create" a Site instance to use in test methods? I have a controller that does slightly different things depending on which Site is accessing it, but Site.getName() is always null in my test methods. It doesn't seem to be possible to do a Site newSite = new Site(), so what is the correct way to do this?

 

Thanks,

Tom

Best Answer chosen by Admin (Salesforce Developers) 
BulentBulent
You can implement a “runAs” that’s sets the site context. 

All Answers

BulentBulent
Site methods only returns value with in the site context (when the method is accessed via the site).
EchoEchoEchoEcho
Okay, but then how are we supposed to unit test those areas of our code?
BulentBulent
You can implement a “runAs” that’s sets the site context. 
This was selected as the best answer
EchoEchoEchoEcho
Ah, thanks!
Suraj SinghSuraj Singh
#Bulent How we can inplement this. Please provide any peice of code.