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
Ajosh JohnAjosh John 

Test Class Coverage for Apex Class

Hello,

Need help with this this test class code coverage.

Apex Class:
public class sendAccountOwnerChangeRequest
{
    public Id referredAccountReqId;
    public Account st;
    public String approvalComments{get;set;}
    public sendAccountOwnerChangeRequest(ApexPages.StandardController controller)
    {
        list<Account> colld;
        referredAccountReqId=controller.getId();
        if(referredAccountReqId!= null){
        colld=[select recordtype.name, Name,In_Approval_Queue__c,BillingState,BillingCountry,Request_Record__c, 
        Area_Code__c,Owner.Email,OwnerId,Locked_to_Owner__c from Account where Id=:referredAccountReqId];
        
        }
         if(colld != null && !colld.isEmpty()){
          st=colld[0];
        }
    }
    
    public Pagereference doCancel()
    {
        Pagereference pgref=new Pagereference('https://cambridgefx.lightning.force.com/lightning/page/home');
        pgref.setRedirect(true);
        return pgref;
    }
    
    public Pagereference updateowner()
    {
        Integer userProspects = [ SELECT COUNT()FROM Account WHERE (RecordType.Name = 'Prospect' or RecordType.Name = 'Prospect Read Only') AND Account.OwnerId = : Userinfo.getUserId()];
        system.debug('User Prospect is --->'+userProspects);
        User u=[select Prospect_Maximum__c from user where Id=: Userinfo.getUserId() Limit 1];
        system.debug('User Prospect Max is --->'+u.Prospect_Maximum__c);
        if(st.Request_Record__c.containsIgnoreCase('TAKE')){
            
            if(st.recordtype.name!='Prospect'){
            
            
            if(userProspects > u.Prospect_Maximum__c){
    st.addError('ApxCl:002 - Prospect Max -- The User has reached their limit of Prospects. ' + 'Prospects owned - ' + userProspects + ', Limit - ' + u.Prospect_Maximum__c +'.');

    }
            AccountTeamMember objAccountTeamMember= new AccountTeamMember();
            objAccountTeamMember.AccountId=referredAccountReqId;
            objAccountTeamMember.UserId=Userinfo.getUserId();
            insert objAccountTeamMember;
            }
            
            else if(st.recordtype.name =='Prospect' && userProspects > u.Prospect_Maximum__c){
    st.addError('ApxCl:002 - Prospect Max -- The User has reached their limit of Prospects. ' + 'Prospects owned - ' + userProspects + ', Limit - ' + u.Prospect_Maximum__c +'.');

    return null;
    }
    
            else {
             st.OwnerId=Userinfo.getUserId();

            }
            update st;
            PageReference acctPage = new PageReference('/'+referredAccountReqId);
            return acctPage;
            }
        else{
            return null;
        }
    }

Test Class:

@isTest
private class TestAccountOwnerChangeRequest {

    static testMethod void myUnitTest() {
        ID profileId=[select Id from profile where Name like'%Administrator%' Limit 1].Id;
        ID role_Id=[select Id from UserRole where Name like'%Administration%' Limit 1].Id;
        
        User u1 = new User();
        u1.Username = 'unit_test1@cambridgefx.com.test1';
        u1.Email = 'unit_test1@cambridgefx.com.uat';
        u1.ProfileId=profileId;
        u1.UserRoleId=role_Id;
        u1.LastName='Test';
        u1.Alias='test';
        u1.Country='USA';
        u1.City='New York';
        u1.Prospect_Maximum__c=100;
        insert u1;
        
        User u2 = new User();
        u2.Username = 'unit_test2@cambridgefx.com.test2';
        u2.Email = 'unit_test2@cambridgefx.com.uat';
        u2.ProfileId=profileId;
        u2.UserRoleId=role_Id;
        u2.LastName='Test';
        u2.Alias='test';
        u2.Country='USA';
        u2.City='New York';
        u1.Prospect_Maximum__c=100;
        insert u2;
        
        System.runAs(u2) {
        Account acc=new Account();
        acc.BillingCity='Test Vegas';
        acc.BillingState='ON';
        acc.BillingCountry='CAN';
        acc.Name='Unit Test Account';
        acc.OwnerId=u1.Id;
        acc.RecordType.Name='Prospect';
        insert acc;  
        
        Task tsk=new Task();
        tsk.Subject='Call';
        tsk.OwnerId=u1.Id;
        tsk.WhatId=acc.Id;
        tsk.ActivityDate=system.today()-180;
        tsk.Status='Completed';
        insert tsk;
        
        Test.startTest();
        PageReference pageRef= Page.SendAccountOwnerShipChangeRequest;
        Test.setCurrentPage(pageRef);
        sendAccountOwnerChangeRequest acontroller = new sendAccountOwnerChangeRequest(new ApexPages.StandardController(acc));
        PageReference pageRef1=acontroller.updateowner();
        
        pageRef1=acontroller.doCancel();
        acontroller.updateowner();

        PageReference pageRef2= Page.SearchCompanies;
        Test.setCurrentPage(pageRef2);
        ControllerSearchCompanies acontroller1 = new ControllerSearchCompanies();
        acontroller1.matchAll =false;
        acontroller1.matchAny =true;
        acontroller1.errorMessageText='test';
        acontroller1.conditionText='test';
        acontroller1.companynameText='test';
        acontroller1.phoneText='test';
        acontroller1.websiteText='test';
        acontroller1.stateText='test';
        acontroller1.countryText='test';
        acontroller1.nameText='test';
        acontroller1.domainText='test';
        acontroller1.streetText='test';
        acontroller1.companynameCheck=true;
        acontroller1.phoneCheck=true;
        acontroller1.websiteCheck=true;
        acontroller1.stateCheck=true;
        acontroller1.countryCheck=true;
        acontroller1.domainCheck=true;
        acontroller1.nameCheck=true;
        acontroller1.streetCheck=true;
        acontroller1.doSearch();

    Test.stopTest();
        }
    }

}