i am facing problem with getting network id in test class, while executing visual force page, controller returns proper network id, when try to write a test class for this 'ClassNetwork' controller, community network id always return null. Can anyone help me?
class ClassNetwork {
//some code
public id fetchNetworkId() {
system.debug('network id ' + Network.getNetworkId()); // network id is null;
return Network.getNetworkId(); // return null
}
// some code
}
@isTest(SeeAllData=true)
public static void TestEmptySearchQuery() {
Test.startTest();
User u=[select Id,name from user where name ='Alex'];
// Registered user from org, same user i login to org through visual force page its working fine. i get the network id in this case.
System.runAs ( u ) {
system.debug('Network communityId ****' + Network.getNetworkId()); //display null
ClassNetwork obj = new ClassNetwork();
Id Nid=obj.fetchNetworkId(); // return null;
system.debug('network id ' + Nid); // Nid is null
}
Test.stopTest();
}