0

My class is using with sharing keyword and method has a query on object which system admin only has access.

List<Login__c> recList = [select id, clientId__c, clientsecret__c from login__c];

So in my test class to test for multiple profiles I am using below:

User u = [select id from user where profile.name != 'System Administrator' Limit 1];
system.runAs(u) {
    MainClass.retrieveQuery();
}

I expect above to throw error when method is called as query in that method can only be run succesfully by system admin but it doesnot throw any error.

However, if I pass specific profile name which is not system admin as below, it runs as expected throws an exception "List has no rows for assignment to sObject"

User u = [select id from user where profile.name = 'Integration User' Limit 1];
system.runAs(u) {
    MainClass.retrieveQuery();
}

My another question is how to catch list has no exceptions error. I tried below:

List<Login__c> recList = new List<Login__c>();
try {
    recList = [select id, clientId__c, clientsecret__c from login__c];
} catch(QueryException qe) {
    system.debug('Query exception: '+qe);
} catch(Exception e) {
    system.debug('Exception: '+e);
}

Even though above code throws exception "List has no rows exception" when run by non-system admin profile, this exception is not caught in either catch block.

  • 2
    (1) always a good idea to select a user that has IsActive = true; (2)recList = [select id, clientId__c, clientsecret__c from login__c]; will never throw query has no rows exception; it will return 0 rows – cropredy Jan 5 '18 at 2:14
  • 1
    help.salesforce.com/articleView?id=000159853&type=1 . Also , use a list and always check if it empty or not before doing something with it. – SfdcBat Jan 5 '18 at 2:20
  • 1
    Your query - [select id from user where profile.name = 'Integration User' Limit 1]; does not return any user so the exception where as select id from user where profile.name != 'System Administrator' Limit 1 returns a value so no exception. Nothing to do with system.runAs or with sharing keyword here. – SfdcBat Jan 5 '18 at 2:31
  • Thanks sfdcbat and cropredy.. thats the exact issue here, my org has no active non-system admin users.. – sf_user Jan 5 '18 at 2:38
4

You only get an error that List has no rows for assignment to SObject if you assign to a single record:

static testmethod void demonstration()
{
    QueryException expectedException;

    Test.startTest();
        try
        {
            User record = [SELECT Id FROM User WHERE Id = null];
        }
        catch (QueryException q)
        {
            expectedException = q;
        }
    Test.stopTest();

    system.assertNotEquals(null, expectedException, 'An exception should be thrown');
}

You are assigning to a list, so an empty query result is not an exception case. Whether you use system.runAs or not has nothing to do with the behavior difference you observe here.

  • Thanks Adrian. What about the system.runAs not throwing any exception when querying as non-system-admin profile ? – sf_user Jan 5 '18 at 2:19
  • 2
    It won't throw an exception because you are assigning to a list...You're comparing two different changes and blaming the wrong one for the difference in observed behavior. – Adrian Larson Jan 5 '18 at 2:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.