+ Start a Discussion
Ramk123Ramk123 

Apex Test Code Coverage Help

Dear Experts,

May I request you to help with increasing code coverage for the following highlighted missing code coverage? Appreciate your quick help.
Apex Class
public class ApexUtility {
    
    public static Boolean isCommunity() {
        if(Site.getSiteId() != null)
            return true;
        return false;
    }

    public static final String PUBLIC_USERNAME = 'Community Site Guest User';
    static User publicUser;
    public static Boolean isLoggedInUser() {
        if(publicUser == null){
            publicUser = [SELECT Id FROM User WHERE Name = :PUBLIC_USERNAME LIMIT 1];
        }
        if(publicUser != null){
            if(UserInfo.getUserId() != publicUser.Id)
                return true;
        }    
        return false;
    }
    
    public static PageReference redirectToVipForm(String recordId) {
        String returnUrl = '/apex/FormWizard?id=' + recordId;
    
        if(isCommunity()){
             returnUrl = '/mcr' + returnUrl;
        }
           
        PageReference p = new PageReference(returnUrl);
        p.setRedirect(true);
        return p;  
    }
    
}

Test Class
 
@isTest
public class btn_EnableCustomerCommunityUser_ExtTest {

    @isTest
    static void testCreateCustomerUser() {
        Account testAccount = TestDataFactory.getPersonAccount();
        insert testAccount;
        
        Test.startTest();
        ApexPages.StandardController stdController = new ApexPages.StandardController(testAccount);
        btn_EnableCustomerCommunityUser_Ext ext = new btn_EnableCustomerCommunityUser_Ext(stdController);
        
        PageReference pr = ext.CreateCustomerCommunityUser();
        Test.stopTest();
        
        testAccount = [SELECT Id, Community_User__c FROM Account WHERE Id = :testAccount.Id LIMIT 1];
        
        System.assertEquals(true, testAccount.Community_User__c);
    }

}

 
mukesh guptamukesh gupta
Hi Ram,

Please use below code
 
@isTest
public class btn_EnableCustomerCommunityUser_ExtTest {

    @isTest
    static void testCreateCustomerUser() {
        Account testAccount = TestDataFactory.getPersonAccount();
        insert testAccount;
        
        Test.startTest();
        ApexPages.StandardController stdController = new ApexPages.StandardController(testAccount);
        btn_EnableCustomerCommunityUser_Ext ext = new btn_EnableCustomerCommunityUser_Ext(stdController);
        
        PageReference pr = ext.CreateCustomerCommunityUser();
        Test.stopTest();
        
        testAccount = [SELECT Id, Community_User__c FROM Account WHERE Id = :testAccount.Id LIMIT 1];
        
        System.assertEquals(true, testAccount.Community_User__c);
    }
	
	@isTest
    static void testCreateCustomerUser_2() {
        Profile prof = [select id from profile where name LIKE '%Community%'];
		User user = new User();
		user.firstName = 'Community Site';
		user.lastName = 'Guest User';
		user.profileId = prof.id,username = 'test@test.com';
		user.email = 'test@test.com';
		insert user;
		system.runAs(user){
			ApexUtility.publicUser = null;
			ApexUtility.isLoggedInUser();
		}
    

}

if you need any assistanse, Please let me know!!


Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
Suraj Tripathi 47Suraj Tripathi 47

Hi

Please find the solution with 83% coverage.

public static testmethod void AccTest(){ 
         Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator']; 
        User u = new User(Alias = 'test', Email='TestData@testorg.com', 
            EmailEncodingKey='UTF-8',FirstName='Community Site Guest', LastName='User', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = p.Id, 
            TimeZoneSidKey='America/Los_Angeles', UserName='TestData@testorg.com');
			insert u;
			
			
			Account ac=new Account();
			ac.Name='Test';
			insert ac;
        
        system.debug('u:::'+u);
        Test.startTest();
        ApexUtility.isCommunity();
        ApexUtility.isLoggedInUser();
        ApexUtility.redirectToVipForm(ac.Id);
        Test.stopTest();
    }


Please mark it as the Best Answer so that other people would take reference from it.

Thank You.

Ramk123Ramk123
Dear Mukesh/Suraj, Thank you very much for providing the best solution!
Ramk123Ramk123
User-added image

Above is the Test Failure in isLoggedInUser while deploying. Could you please suggest? 
Suraj Tripathi 47Suraj Tripathi 47
Actually, I am using the same code and it is showing 83% coverage

User-added image
Please share your whole code here with the test class
Ramk123Ramk123
Hi Suraj,

Thanks for the response!

The code is the same. I can also see the 83% code coverage. Throwing the test class validation error when deploying to production. User-added image