How to Create Profile in test method?

I developed my code in my DE and created a custom profile named “XYZ”. I used that profile in my Apex class and test methods.

While installing that managed beta package in my sandbox my test method failed because that profile is not visible.

Can I create a custom profile in the test method to ensure success?

Answer

No you cannot create profiles in apex code, either through tests or otherwise, this is presumably a security limitation.

The second part of your problem is something I have also encountered. You cannot include a profile in a Managed Package. You can include ‘Profile Settings’ which can be applied to a subscribers existing profiles. But the name of the subscribers profile will not always match the name of the profile you are referencing in your unit test.

What I have done to eliminate this problem in the past is rather than referencing a profile in code, I use the Schema Describe information to determine the object or field level permissions that implicitly grant the user access. For example if a user has write permission on my Milestone Object, my code lets them perform a priviledged action on the Milestone

Attribution
Source : Link , Question Author : Axaykumar Varu , Answer Author : Daniel Blackhall

Leave a Comment