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
Austin GutzAustin Gutz 

Apex Test Class Fails

I'm trying to deploy a very simple Apex class that I'll then schedule to do a nightly record update. The deployment is failing because of errors with a test class that came from an installed app. I'm an admin, so I have very little Apex knowledge.

The errors are all:
System.QueryException: List has no rows for assignment to SObject 

All the errors point to a line with:   User u = getUser('NotAccessible1');

From what I'm reading it has something to do with an empty query. I just don't know how to fix it. I'll post the code in a different comment. 
Austin GutzAustin Gutz
@IsTest
public class CaseStatusCahngeTriggerHandlerTest {

    @testSetup static void setupTestdata() {
        
        Case newCase = new Case();
        newCase.Subject = 'Unittest';
        newCase.Status = 'New';
        insert newCase; 
        
        Case testCase = [select Subject, Status from Case where Subject = 'Unittest']; 
        System.assertEquals(testCase.Subject, 'Unittest');
    }
    
    
    
    @IsTest static void testOnAfterInsert(){

        Case[] testCase = [select Subject, CaseNumber, Status, Owner.Name from Case where Subject = 'Unittest'];
        
        CaseStatusChangeTriggerHandler.OnAfterInsert(testCase);
        
        Case_Status_Change__c[] caseStatusChange = [select Name from Case_Status_Change__c where Case__r.Id =:testCase[0].Id];
        
        System.assertEquals(caseStatusChange[0].Name, testCase[0].CaseNumber + ' status: New');
    
    }
    
    
    @IsTest static void testOnAfterUpdate(){

        Map<Id, Case> oldObjectMap = new Map<Id, Case>();
          
        Case[] testCase = [select Subject, CaseNumber, Status, Owner.Name from Case where Subject = 'Unittest'];
        
        Case_Status_Change__c  statusChange = new  Case_Status_Change__c();
        statusChange.Name = testCase[0].CaseNumber + ' status: New';
        statusChange.Case__c = testCase[0].Id;
        statusChange.Status_Name__c = testCase[0].Status;
        statusChange.Set_Time__c = Datetime.now();
        insert statusChange;
        
        testCase[0].Status = 'Escalated';

        Case oldCase = new Case();
        oldCase.Subject ='Unittest';
        oldCase.Status = 'New';
        oldCase.Id=testCase[0].Id;
        oldObjectMap.put(testCase[0].Id, oldCase);

        
        CaseStatusChangeTriggerHandler.OnAfterUpdate(testCase, oldObjectMap);
        
        Case_Status_Change__c[] caseStatusChange = [select Name from Case_Status_Change__c where Case__r.Id=:testCase[0].Id and Change_Time__c = null];
        
        
        System.assertEquals(caseStatusChange[0].Name, testCase[0].CaseNumber + ' from New to Escalated');
    
    }
     
    /**
     *Tests if the user does not have access to Case and Case_Status_Change__c objects
     */
    @IsTest static void testAccessible1(){
       
        User u = getUser('NotAccessible1');

        System.runAs(u) {
            try {
                testOnAfterInsert();
                System.assert(false, 'fail the test if no exception thrown ');
            }
            catch (QueryException e) {
                 System.assertEquals(e.getMessage(), 'Insufficient access to Case or User objects');         
            }   
        }
  
    }
    
    @IsTest static void testAccessible2(){
        
        User u = getUser('NotAccessible2');
        
        System.runAs(u) {
            try {
                testOnAfterUpdate();
                System.assert(false, 'fail the test if no exception thrown ');
            }
            catch (QueryException e) {
                 System.assertEquals(e.getMessage(), 'Insufficient access to Case Status Change or business hours objects');         
            }   
        }
    }
    
    @IsTest static void testCreatable(){
        
        User u = getUser('NotCreatable');
        
        System.runAs(u) {
            try {
                testOnAfterInsert();
                System.assert(false, 'fail the test if no exception thrown ');
            }
            catch (DmlException e) {
                 System.assertEquals(e.getMessage(), 'Insufficient permissions to create Case Status Change');         
            }    
        }
        
        System.runAs(u) {
            try {
                testOnAfterUpdate();
                System.assert(false, 'fail the test if no exception thrown ');
            }
            catch (DmlException e) {
                 System.assertEquals(e.getMessage(), 'Insufficient permissions to create Case Status Change');         
            }    
        }     
    }
    
    @IsTest static void testUpdatable(){
        
        User u = getUser('NotUpdatable');
        
        System.runAs(u) {
            try {
                testOnAfterUpdate();
                System.assert(false, 'fail the test if no exception thrown ');
            }
            catch (DmlException e) {
                 System.assertEquals(e.getMessage(), 'Insufficient permissions to update Case Status Change');         
            }
            
        }      
    }
    
    
    public static User getUser(String profileName) {
        
        Profile p = [SELECT Id FROM Profile WHERE Name=:profileName limit 1];
        
        String testemail = 'atest@test.demo';
        User pu = new User(profileId = p.Id, username = testemail, email = testemail, 
                           emailencodingkey = 'UTF-8', localesidkey = 'en_US', 
                           languagelocalekey = 'en_US', timezonesidkey = 'America/Los_Angeles', 
                           alias='cspu', lastname='lastname');
        
        return pu;
        
        
    }
    
}

 
devedeve
Hi Austin,

Please check profile with NotAccessible1 name exists in org because this error comes when query return null value.

Thanks
Austin GutzAustin Gutz
So I found some more info about the issue from a comment someone left on the AppExchange Review section:

"The test class won't pass tests
Class CaseStatusCahngeTriggerHandlerTest (yes, misspelled) won't pass tests in your org because it assumes you have Profilees named 'NotAccessible1', 'NotAccessible2', 'NotCreatable' and 'NotUpdatable' with varying levels of CRUD access to User, BusinessHours, and Case_Status_Change__c objects. 

You need to make test class without sharing; and in testSetup, create the Case using a system admin user.

You can fix test: 'testAccessible1' by using Profile 'Force.com - One App User'. You can fix test 'testAccessible2' by using profile 'Read Only'. Personally, I'd comment out the testCreatable and testUpdatable test methods if not also testAccessible1 and testAccessible2"

Would it be easiest to use the suggestion to comment out those test methods? If yes, how do I do that?