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
bbrantly1bbrantly1 

Test Method for isCurrentUserLicensed

Hello

 

I'm trying to write a test method for isCurrentUserLicensed

 

In my class's Constructor I do a couple of checks to make sure the user is licensed for other tools that are needed by this tool.

 

Example:

 

public ClassName() { if (!UserInfo.isCurrentUserLicensed('TOOLONE')) { isAbleToUseTool = false; LicenseError = 'Error HERE' } else if (!UserInfo.isCurrentUserLicensed('TOOLTWO')) { sAbleToUseTool = false; LicenseError = 'Error HERE' } else { //Excecute Constructor } }

 

My Test Method is as Follows:

 

 

public static testMethod void ClassNameTest() { /////////////////////////////////////////////////////////////// //Create User Profile p = [SELECT Id FROM profile WHERE name='Standard User']; User u2 = new User(); u2.alias = 'newUser'; u2.email='newuser@testorg.com'; u2.emailencodingkey='UTF-8'; u2.lastname='Testing'; u2.languagelocalekey='en_US'; u2.localesidkey='en_US'; u2.timezonesidkey='America/Los_Angeles'; u2.username='newuser@testorg.com'; u2.profileid = p.Id; insert u2; u2 = [SELECT Id FROM User WHERE email='newuser@testorg.com']; ////////////////////////////////////////////////////////////// System.runAs(u2) { ClassName cNObj = new ClassName(); //RUN OTHER TEST METHODS.... } }

 

 

Becuase the "fake" user created isn't license for the packages i'm testing against in the isCurrentUserLicensed the rest of the functions in the class of course fail.

 

What is the proper way to create a test method for this scenario? Should I be using NoAccessException?

 

Thanks for any help offered.

 

Best Answer chosen by Admin (Salesforce Developers) 
bbrantly1bbrantly1

Here is my current work around so I can get the test methods to run correctly.

 

public static testMethod void ClassNameTest() { /////////////////////////////////////////////////////////////// //Create User Profile p = [SELECT Id FROM profile WHERE name='Standard User']; User u2 = new User(); u2.alias = 'newUser'; u2.email='newuser@testorg.com'; u2.emailencodingkey='UTF-8'; u2.lastname='Testing'; u2.languagelocalekey='en_US'; u2.localesidkey='en_US'; u2.timezonesidkey='America/Los_Angeles'; u2.username='newuser@testorg.com'; u2.profileid = p.Id; insert u2; u2 = [SELECT Id FROM User WHERE email='newuser@testorg.com']; ////////////////////////////////////////////////////////////// System.runAs(u2) { ClassName cNObj = new ClassName(); cNObj.ClassNameConstructor(); //RUN OTHER TEST METHODS.... } } public ClassName() { if (!UserInfo.isCurrentUserLicensed('TOOLONE')) { isAbleToUseTool = false; LicenseError = 'Error HERE' } else if (!UserInfo.isCurrentUserLicensed('TOOLTWO')) { sAbleToUseTool = false; LicenseError = 'Error HERE' } else { //Excecute Constructor

ClassNameConstructor(); } } public void ClassNameConstructor() { sAbleToUseTool = true; //Excecute Constructor }

 

 

I think there should be a way to test against the isCurrentUserLicense function, but until that is out there I would recommend this workarond.

All Answers

bbrantly1bbrantly1

Update:

 

I put the constructor code in a try/catch block and even though the error is below:

 

"caused by: System.Exception: No such column 'ID' on entity ..."

 

None of the Exception types, (Exception, NoAccessException, NoDataFoundException, QueryException, etc. [I Tried all of them]) seem to catch the error.

bbrantly1bbrantly1

Here is my current work around so I can get the test methods to run correctly.

 

public static testMethod void ClassNameTest() { /////////////////////////////////////////////////////////////// //Create User Profile p = [SELECT Id FROM profile WHERE name='Standard User']; User u2 = new User(); u2.alias = 'newUser'; u2.email='newuser@testorg.com'; u2.emailencodingkey='UTF-8'; u2.lastname='Testing'; u2.languagelocalekey='en_US'; u2.localesidkey='en_US'; u2.timezonesidkey='America/Los_Angeles'; u2.username='newuser@testorg.com'; u2.profileid = p.Id; insert u2; u2 = [SELECT Id FROM User WHERE email='newuser@testorg.com']; ////////////////////////////////////////////////////////////// System.runAs(u2) { ClassName cNObj = new ClassName(); cNObj.ClassNameConstructor(); //RUN OTHER TEST METHODS.... } } public ClassName() { if (!UserInfo.isCurrentUserLicensed('TOOLONE')) { isAbleToUseTool = false; LicenseError = 'Error HERE' } else if (!UserInfo.isCurrentUserLicensed('TOOLTWO')) { sAbleToUseTool = false; LicenseError = 'Error HERE' } else { //Excecute Constructor

ClassNameConstructor(); } } public void ClassNameConstructor() { sAbleToUseTool = true; //Excecute Constructor }

 

 

I think there should be a way to test against the isCurrentUserLicense function, but until that is out there I would recommend this workarond.

This was selected as the best answer