0

I need help in writing test class for following apex class. Could anyone please suggest how to create test data,test the following method defined in apex class with positive and negative scenarios and use of System.assertEquals() to make sure each scenario had the expected outcome.

customAppName field is multiselect picklist on customObject

public with Sharing class abcService{
  @AuraEnabled 
  public static List<customObject> xyzMethod(){   
      UserAppInfo userAppInfo = [SELECT Id, AppDefinitionId FROM UserAppInfo WHERE UserId = 
      :UserInfo.getUserId() LIMIT 1];
      AppDefinition appDefinition = [SELECT DurableId, Label FROM AppDefinition Where DurableId = 
      :userAppInfo.AppDefinitionId LIMIT 1];
      String appName = appDefinition.Label;
      System.debug('appName------->' + appName);
      return [SELECT custom fields FROM customObject  WHERE customAppName includes (:appName)];        
  }
}

When I started creating test data got fields not writeable issue for UserAppInfo and AppDefinition.

Based on the comments mentioned in the post, I started with test class but not able to cover the scenarios.

@isTest
public class abcServiceTest{

   @testSetup
   public static void testSetup(){
    
   }

@isTest static void testPositiveScenario(){ //Passing App name
    
    User u = someClass.testUserCreate();
    System.runAs(u){
        
        
        Test.startTest();
        
        customObject__c customObjectObj = new customObject__c(custom_Id__c = 'Id_01', custom_Name__c = 'Test Name AA', customAppName__c = 'Test App AA');
        insert customObjectObj;
        List<customObject__c> customList = [SELECT custom_Id__c, custom_Name__c, customAppName__c FROM customObject__c WHERE customAppName__c INCLUDES ('Test App AA')];
        System.debug('customList---------->' + customList);
        System.assertEquals(1, customList.size());
     
        Test.stopTest();
    }   
}

@isTest static void testNegativeScenario(){
    
   User u = someClass.testUserCreate();
    System.runAs(u){
        Test.startTest();
        
        customObject__c customObjectObj = new customObject__c(custom_Id__c = 'Id_02', custom_Name__c = 'Test Name BB', customAppName__c = 'Test App BB');
        insert customObjectObj;
        List<customObject__c> customList = [SELECT custom_Id__c, custom_Name__c, customAppName__c FROM customObject__c WHERE customAppName__c INCLUDES ('Test App CC')];
        System.debug('customList---------->' + customList);
        System.assertEquals(0, customList.size());
        
        Test.stopTest();
    }   
}    
}

Could anyone suggest how to have xyzMethod() covered. Any kind of help is appreciated.

4
  • You have several questions here. We do ask that each post focus on just one. Your questions about how to build test classes and write assertions are answered in our canonical resources, found under the canonical-qa tag. Your question about how to control the test environment when working with metadata objects is more complex. If you search SFSE, you'll find a number of questions about mocking these objects using dependency injection and JSON deserialization. These are advanced techniques and may require more research and learning. – David Reed Nov 27 '20 at 15:00
  • Would it possible to share approach and guidance to begin writing test class for above method. I do understand that these are metadata objects. – Rose Nov 27 '20 at 16:45
  • @DavidReed, I started writing test class with positive and negative scenarios but not able to cover method. – Rose Nov 27 '20 at 17:08
  • Your test class does not invoke the code under test. Please take a look at the linked questions in our canonical-qa library to learn about the core techniques of unit testing. – David Reed Nov 27 '20 at 17:31
1

I have not worked with those objects, but note that not all infrastructure related data can be modified. For the cases where they cannot, you either have to construct the test to work with no records, or if the objects are in the group that are visible make assumptions about the data. Or accept that a unit test can't be created.

Note that this:

UserAppInfo userAppInfo = [
    SELECT Id, AppDefinitionId
    FROM UserAppInfo
    WHERE UserId = :UserInfo.getUserId()
    LIMIT 1
];

looks questionable as there may well be zero (causing an error), 1 (works) or many (meaning you will get an arbitrary one selected) records.

3
  • based on the solution suggested, I started writing test class with positive and negative scenarios. It passed but still not able to cover xyzMethod(). Would it possible to share some approach or guidance to have this issue resolved. – Rose Nov 27 '20 at 17:10
  • You cave to call the method xyzMethod from your test. I suggest you first work on some simple examples from Trailhead before working in this more awkward than usual case. This is not a training forum: it is focussed on specific questions and answers. – Keith C Nov 27 '20 at 18:17
  • I did that and got error as list has no rows for assignment. – Rose Nov 27 '20 at 18:49

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