1

I have following class:

public without sharing class exampleController{
    public static User currentUser {
        get {
            if(currentUser == null){
                currentUser = [SELECT Id,
                    Email,LastLoginDate,CompanyName,Contact.Name,Phone,City,State,PostalCode,CreatedDate,LanguageLocaleKey,
                    CustomerCareDownloadsCount__c, CustomerCareLoginCount__c, Contact.Language_EncodingKey__c,
                    ContactId,Contact.AccountId,Contact.Order_Visibility__c, Contact.Invoice_Visibility__c,Contact.eCommerce_Visibility__c, CustomerNo for SAP invoices webservices FROM User WHERE Id=:UserInfo.getUserId()];
            }

            return currentUser;
        }
        public set;
    }
}

How to cover currentUser in test class.I write test class but covered nothing.I wrote like below:

@isTest
public class TestexampleController {
static testmethod void exampleControllerTestMethod(){
                Account oAccount = new Account();
                oAccount.Name = 'MyRecords Controller Testing';
                oAccount.BillingCountry = 'USA';
                oAccount.SAP_Bill_To_Account_Number__c = '043343334';
                oAccount.SAP_Ship_To_Account__c = '043343335';
                insert oAccount;

                Contact oContact = new Contact();
                oContact.LastName = 'MyportaUser';
                oContact.AccountId = oAccount.Id;
                oContact.Status__c = 'Active';
                oContact.Order_Visibility__c = 'Yes';
                oContact.Invoice_Visibility__c = 'Yes';
                oContact.eCommerce_Visibility__c = 'Yes';
                insert oContact;

                Profile p = [SELECT Id FROM Profile WHERE Name='someProfile']; 
                User u = new User(Alias = 'storddt' ,Email='myportalOrderuser@testorg.com',CompanyName='saturn',Phone='0555444111',City='bang',State='bg',Country='USA',
                                  EmailEncodingKey='UTF-8', LastName='OrderTesting', LanguageLocaleKey='en_US', 
                                  LocaleSidKey='en_US', ProfileId = p.Id, 
                                  TimeZoneSidKey='America/Los_Angeles', UserName='myportalordercommunity@testorg.com',ContactId=oContact.Id);
                insert u;
        System.runAs(u) {
        exampleController tm = new exampleController();

        }
    }
2

You will have to access the member variables of main class from test class for them to get covered.

You can access the static variable using the below code

User currentUser = exampleController.currentUser;

So, your code will be

System.runAs(u) {
    exampleController tm = new exampleController();
    User currentUser = exampleController.currentUser;
}

Note : Make sure to write the assert statements in test class to verify if the functionality you have developed is working as expected or not.

1
  • Cool! now works good.Thanks.
    – jsmith
    Jul 1 '19 at 6:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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