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
RuniRuni 

Aura

Hi All,

How to write test class for below code.

thanks
Swathi


public class Radius {
  @AuraEnabled
    public static Map<String, Object> getUserDetails1()    {

        Map<String, Object> returnMap = new Map<String, Object>();

        String userId = UserInfo.getUserId();

        User userRecord = [SELECT Id, ContactId, AccountId FROM User WHERE Id =: userId];
        Account personAccRecord = [SELECT  Name,  Phone FROM Account WHERE Id =: userRecord.AccountId];

        returnMap.put('personAccRecord', personAccRecord);
        return returnMap;
    }
}
Best Answer chosen by Runi
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Swathi,

Can you try the below test class for the apex class.
 
@istest
public class RadiusTest {
    @isTest static void testUpsertCase1() {
        Account portalAccount1 = new Account(Name = 'TestAccount');
Database.insert(portalAccount1);

//Create contact
Contact contact1 = new Contact(
    FirstName = 'Test',
    Lastname = 'McTesty',
    AccountId = portalAccount1.Id,
    Email = System.now().millisecond() + 'test@test.com'
);
Database.insert(contact1);

//Create user
Profile portalProfile = [SELECT Id FROM Profile WHERE UserType = 'CSPLitePortal'  Limit 1];
User user1 = new User(
    Username = 'sampleabcdefghi@gmail.com',
    ContactId = contact1.Id,
    ProfileId = portalProfile.Id,
    Alias = 'aat123',
    Email = 'test12ssssa345@test.com',
    EmailEncodingKey = 'UTF-8',
    LastName = 'McTestsasy',
    CommunityNickname = 'test123sss45',
    TimeZoneSidKey = 'America/Los_Angeles',
    LocaleSidKey = 'en_US',
    LanguageLocaleKey = 'en_US'
);
insert user1 ;
    
    
    system.runAs(user1){
        Map<String, Object> returnMap = new Map<String, Object>();
     returnMap= Radius.getUserDetails1();
        
        Account acc=[select Name,phone from Account where Name = 'TestAccount' ];
        system.assertEquals(returnMap.get('personAccRecord'), acc);
    }
    }

}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks
​​​​​​​