I'm missing something obvious but we have a PostInstall script that makes a PermissionSetAssignment to the installer to give an Admin permission. The test class is failing since, as is documented in Salesforce InstallHandler Docs, the special install user is not accessible in tests.

The error is on the insert psalist;

So how do I test that the InstallHandler successfully assigned the Permission Set? Thought of a Login Flow but given that this runs everytime a user logs in its overkill for a one-time process and is the purpose of having a Post Install script.

    global void onInstall(InstallContext context) {
        Map<String, Object> inputs = new Map<String, Object>();
        if (context.previousVersion() == null) {
            // ID of user installing package. Part of InstallHandler Interface
            Id UserId = context.installerId();

            // Getting System Administrator PermissionSet
            List<PermissionSet> PermissionSetToAssignList = [
                SELECT Id, Name
                FROM PermissionSet
                WHERE Name = 'provNpiAdministrator' AND NamespacePrefix = 'prov'

            List<PermissionSetAssignment> psalist = new List<PermissionSetAssignment>();
            PermissionSet PermissionSetToAssign = PermissionSetToAssignList[0];
            PermissionSetAssignment psa = new PermissionSetAssignment();
            psa.PermissionSetId = PermissionSetToAssign.Id;
            psa.AssigneeId = UserId;

            insert psalist;
        } else if (context.isUpgrade()) {
        if (context.isPush()) {
class PostInstallClassNpiTest {
    static void onInstallTest() {
        PostInstallClassNpi postinstall = new PostInstallClassNpi();
        Test.testInstall(postinstall, null);
        List<PermissionSetAssignment> psa = [
            SELECT id, PermissionSetId
            FROM PermissionSetAssignment
            WHERE PermissionSet.Name = 'provNpiAdministrator'
        System.assertEquals(psa.size(), 1, 'PermissionSetAssignment found');
        // Test.testInstall(postinstall, new Version(1, 0), true);

1 Answer 1


You're meant to set up a context user first. Here's a trivial example I wrote.

@isTest class installerTest {
    @isTest static void test() {
        User u = [select Fields(standard) from user where isactive=true limit 1];
        u = u.clone(false, false, false);
        System.runas(u) {
            Test.testInstall(new OnInstallH(), null);
            System.assertEquals(UserInfo.getUserId(), storage.userId);
        System.assertNotEquals(UserInfo.getUserId(), storage.userId);

Where storage.userId is just a static variable somewhere for testing.

This runs without error, so presumably it should be safe to say that your code should be able to create a test user, assign it a particular profile, and then have that user run the install, which shouldn't fail during testing.

  • This is probably what was needed but I ended up using a Flow instead for postinstall actions.
    – ddeve
    Commented May 5, 2023 at 2:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .