Skip to main content
I'm having trouble developing the test class for this apex web service class, I'm a novice developer. can you help me? thank you

 

 

public class DI_DynBtnCmpCnt {

@AuraEnabled

public static user fetchUser(){

// query current user information

User oUser = [select id,AccountId, Contact.DI_Proposed_Inspector__c, Name,TimeZoneSidKey,Username,Alias,Country,Email,FirstName,LastName,IsActive,IsPortalEnabled

FROM User Where id =: userInfo.getUserId()];

return oUser;

}

@AuraEnabled

public static String getListId(String name){

return [SELECT Id, Name, DeveloperName, SobjectType FROM ListView where DeveloperName=: name].Id;

}

@AuraEnabled

public static ServiceResource fetchInspector(){

// query current ServiceResource information

ServiceResource sr = [select id, UserID__c

FROM ServiceResource Where UserID__c =: userInfo.getUserId() LIMIT 1];

return sr;

}

}

 

 
2 个回答
  1. 2021年4月20日 17:51
    Hi Luca ,

     

    Sample test class for your code.

     

    1. Testsetup method is to set data needed to test the logic of our class .i.e. for your code You need to add profiles first then create user record ,  list view,Service Resource

     

    2. testMethod1() method is from where we call the class method codes and use system.asserts to verify if the output matches the expected output .

     

    note :  you may need to replace id values referenced in the below code and use some string value common for all org . 

     

    @isTest 

     

    public class DI_DynBtnCmpCntTest {

     

        @testSetup  static void setup() {

     

         

     

            User userToCreate = new User();

     

           

     

           // Do you recognize these fields?

     

           userToCreate.FirstName = 'David';

     

           userToCreate.LastName  = 'Liu';

     

           userToCreate.Email     = 'dvdkliu+sfdc99@gmail.com';

     

           userToCreate.Username  = 'sfdc1-dreamer@gmail.com';

     

           userToCreate.Alias     = 'fatty';

     

           userToCreate.ProfileId = '00e7F00000391N4';

     

           // Don't worry about these

     

           userToCreate.TimeZoneSidKey    = 'America/Denver';

     

           userToCreate.LocaleSidKey      = 'en_US';

     

           userToCreate.EmailEncodingKey  = 'UTF-8';

     

           userToCreate.LanguageLocaleKey = 'en_US';

     

           insert userToCreate;

     

        }

     

    @isTest static void testMethod1() {

     

       

     

        DI_DynBtnCmpCnt.fetchUser();

     

       System.assertEquals('00B7F00000EiyxEUAR',DI_DynBtnCmpCnt.getListId('PublicReports'));

     

    ;

     

    }

     

    }

     

    if this helpful , please mark it as best answer. thank you
0/9000