-1

I am writing a test class for the simple Apex class with SOQL query but I am getting following error while Run test.

System.QueryException: List has no rows for assignment to SObject

My Apex class is as below

    public class OlympicReward_ProgressBar {

    @AuraEnabled
    public static Olympic_Rewards__c getOlympicReward_ProgressBar(){


       Olympic_Rewards__c olr = [SELECT Id, Name, Reward_Bar__c,Agency_stat_code__c,Contest_period_production__c,Current_Reward_Level__c,Agency__c 
                                 from Olympic_Rewards__c 
                                 WHERE Agency__c IN 
                                 (SELECT AccountId From User Where Id = :UserInfo.getUserId()) LIMIT 1];
        return olr;
    }
}

Test Class

 @isTest
private class TestOlympicReward_ProgressBar {
    static testmethod void GetOlympicReward_ProgressBar(){

        // Setup Integration User to run test
        User integrationUser = TestDataUtils.findIntegrationUser();

        System.runAs(integrationUser){

            List<Account> agencyList = TestDataUtils.createAgencyDataForAgencyPlans(1);
            system.debug('@@@ AgencyList Size: ' + agencyList.size());// Debug statement returns Size as 1 

            if(!agencyList.isEmpty()) {
                insert agencyList;
            }

            Account agency = agencyList.get(0); 
            System.debug('@@@ Agency Name' + agency.name); // Not Displaying in Debug log.
            agency.Region__c = 'Illinois Regional Office';
            agency.Marketing_Territory__c = 'IL Territory 1';
            update agency;

            System.debug('@@@ Agency Name' + agency.name); // Not Displaying in Debug log.

            Olympic_Rewards__c olr = new Olympic_Rewards__c();
            olr.Name = 'Test Olympic Reward';
            olr.Agency__c = agency.Id;
            Test.startTest();
            insert olr;

            Olympic_Rewards__c getOlpr = OlympicReward_ProgressBar.getOlympicReward_ProgressBar();
            Test.stopTest();
        }
    }

}

marked as duplicate by Eric apex Jul 21 '17 at 14:59

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    Seems to be an issue with assuming that a User will be found in the method. The method you are testing needs to be reworked as outlined in many answers here around that error. Including handling the scenario where no user is found. – Eric Jul 21 '17 at 14:59
  • I am sorry about duplicate. I was struggling through test class all this time instead of apex class. I will mark this as deletion. Thank you for your help. – Aman Jul 21 '17 at 15:26
  • Well it turned out I cannot delete it myself. So it may require admin to delete this. – Aman Jul 21 '17 at 15:28
  • It also has to do with your test. If you are expecting your test to return a proper user then it is not. The test should still fail unless you are actually testing that the user is not the one you are looking for in your class. When you assert the expected result the test may fail depending on what your assertion is. As it stands now your test is not really testing anything as you do not assert that what you expect to happen actually happened – Eric Jul 21 '17 at 15:29
  • Yes you are, and that is what I am expecting it to fail at the moment I am currently working on getting that corrected by creating the test user as I expect in my apex class and then do assert for that. – Aman Jul 21 '17 at 15:38
3

There is a common strategy to avoid this style of QueryException. Query to a list and return its first element if it's not empty.

List<Olympic_Rewards__c> potentialMatches = [
    SELECT ...
    FROM Olympic_Rewards__c
    WHERE ...
    LIMIT 1
];
return potentialMatches.isEmpty() ? null : potentialMatches[1];
  • Thanks a lot Adrian. That solved my problem. Can't believe it was to do with apex class. I was fighting with test class only all this time. – Aman Jul 21 '17 at 15:22

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