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
Sai Kiran 1846Sai Kiran 1846 

ApexTesting

public without sharing class X311Portal_EditProfileCtrl {
    @AuraEnabled
    public static Map<String, Object> getContactDetails(){
        
        Map<String, Object> returnMap = new Map<String, Object>();
        
        User userRecord = [SELECT Id, AccountId, ContactId FROM User WHERE Id =: UserInfo.getUserId()];
        
        Account personAccRecord = [SELECT FirstName, LastName, PersonEmail, Phone, PersonMailingCity, PersonMailingState,
                                    PersonMailingPostalCode, PersonMailingStreet FROM Account WHERE Id =: userRecord.AccountId];
        
        returnMap.put('personAccRecord', personAccRecord);
        
        return returnMap; 
    }   

    @AuraEnabled
    public static void updateContactDetails(String record) {
        Account accountRecord = (Account) JSON.deserialize(record, Account.class);
        upsert accountRecord;
    }
}
Sai Kiran 1846Sai Kiran 1846
can anyone guide me how to write a test class for the above class?
 
Neha Arora 50Neha Arora 50
In your test class

//Create person account 
Use System.runAs(user){
  //create user record where userId.Account = personAccId
  test.startTest();
    //Invoke your class - X311Portal_EditProfileCtrl.getContactDetails();
  test.stopTest();
}

Let me know if it helps.
AnkaiahAnkaiah (Salesforce Developers) 
Hi Sai,

try with below code.
@istest
public class X311Portal_EditProfileCtrlTest {
    
static testmethod void getContactDetailsmethod(){
        
Set<String> customerUserTypes = new Set<String> {'CSPLiteUser', 'PowerPartner', 'PowerCustomerSuccess',   'CustomerSuccess'};
Account acc = new Account (
phone='345678901',
FirstName ='test acc', 
LastName = 'last', 
PersonEmail = 'abc@test.com',
PersonMailingCity = 'chennai', 
PersonMailingState = 'TamilNadu',
PersonMailingPostalCode ='600119,
PersonMailingStreet = 'test'
);  
insert acc;
string acct = 'newAcc1';
Contact con = new Contact (
AccountId = acc.id,
LastName = 'portalTestUser'
);
insert con;
//UserRole ur = [Select PortalType, PortalAccountId From UserRole where PortalType =:'wipro Customer User' limit 1];
Profile p = [select Id,name from Profile where UserType in :customerUserTypes limit 1];
 
User newUser = new User(
//UserRoleId = ur.Id,
profileId = p.id,
username = 'newUser@yahoo.com',
email = 'pb@ff.com',
emailencodingkey = 'UTF-8',
localesidkey = 'en_US',
languagelocalekey = 'en_US',
timezonesidkey = 'America/Los_Angeles',
alias='nuser',
lastname='lastname',
contactId = con.id,
 country='India'
);
insert newUser; 

 system.runAs(newUser){
 X311Portal_EditProfileCtrl.getContactDetails();         
        }
    
    }

}

If this helps, Please mark it as best answer.

Thanks!!