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
RbnRbn 

Help on Test Coverage

Hi ......

 

Please find the below class:::

public with sharing class coninterpage {
    public Id conId;
    public Id accId;
    public List<RecordType> recType;
    public Account recTypeId;
    public List<contact> con;
    Public String hostName;

    public coninterpage(ApexPages.StandardController controller) {
        hostName = ApexPages.currentPage().getHeaders().get('Host');

    }   
       
    public PageReference inter(){
        Account_Profile_Name__c mc = Account_Profile_Name__c.getValues(userinfo.getProfileId());
        Boolean field = mc.Standard_Pages__c;
        conId = ApexPages.currentPage().getParameters().get('id');
        system.debug('==== '+conId);
        con = [select id, AccountId from contact where id=:conId limit 1];
        system.debug('====@@@@ '+con);
        for(Contact c12:con)
            {
            accId = c12.AccountId; 
            }
        if(accId!=null){
            system.debug('====@@@@ '+con[0].AccountId );
            system.debug('====!!!!! '+accId);

            recTypeId = [SELECT RecordTypeId FROM Account where id=:accId limit 1];
            system.debug('==== $$$$$$'+recTypeId);

            recType = [SELECT Id,Name FROM RecordType where id=:recTypeId.RecordTypeId];
            system.debug('====%%%%%% '+recType);
                
     // The below code will redirect to the PersonTabDetailPage.     
            if(recType[0].Name=='Person')
            {
            if(field)
            {
            pagereference ref = new pagereference('https://'+hostname+'/'+accId);    
            ref.setRedirect(true);
            ref.getParameters().put('nooverride', '1');
            return ref;
            }
        else{
             PageReference pageRef = new PageReference('https://'+hostname+'/apex/PersonTabDetailPage?id='+accId);
                    system.debug('@@@@@@@@@@@@@'+pageRef);
                    return pageRef ;
                    }
                   
             }       
     // The below code will redirect to the conpagelayout.  
            if(recType[0].Name=='Corporation')
            {
            if(field)
            {
            pagereference ref = new pagereference('https://'+hostname+'/'+conId);    
            ref.setRedirect(true);
            ref.getParameters().put('nooverride', '1');
            return ref;
            }
        else{
            PageReference pageRef = new PageReference('https://'+hostname+'/apex/conpagelayout?id='+conId);
             system.debug('@@@@@@@@@@@@@'+pageRef);
             return pageRef ;
            }
         // return null;   
        }
        }
        else{
        if(field)
            {
            pagereference ref = new pagereference('https://'+hostname+'/'+conId);    
            ref.setRedirect(true);
            ref.getParameters().put('nooverride', '1');
            return ref;
            }
        else{
            PageReference pageRef = new PageReference('https://'+hostname+'/apex/conpagelayout?id='+conId);
             system.debug('@@@@@@@@@@@@@'+pageRef);
             return pageRef ;
            }

              
            }
    
    return null;
}
}

 Right now i can cover only upto 26% ...Please help me out in this regard.....

@isTest
public class Test_coninterpage {
     public static testmethod void method2(){
     list<recordtype> rCorp= new List<Recordtype>([select id,DeveloperName  from recordtype where  SobjectType='Account' and DeveloperName ='Corporation' limit 1]);
        Account acc = new Account(Name='bgt',recordtypeid=rCorp[0].id,Status__c='Active',Type = 'Prospect');
        insert acc;
        Contact continter = new Contact(LastName = 'VRaj',AccountId=acc.id);
        insert continter ;
         Contact continter2 = new Contact(LastName = 'VRaj');
        insert continter2 ;
        
        List<Account_Profile_Name__c> Lcs = new List<Account_Profile_Name__c>();
          Account_Profile_Name__c apn = new Account_Profile_Name__c();
          apn.Standard_Pages__c = true;
          Lcs.add(apn);
          
          insert Lcs;
          
          
          ApexPages.StandardController ctrl=new ApexPages.StandardController(continter);
          System.currentPageReference().getParameters().put('id',continter.id);
          coninterpage conint = new coninterpage(ctrl);
          //,RecordTypeId='01260000000DnCTAA0'
         
          conint.inter();    
  ApexPages.StandardController ctrl2=new ApexPages.StandardController(continter2);
          System.currentPageReference().getParameters().put('id',continter2.id);
          coninterpage conint2 = new coninterpage(ctrl2);   
          //conint2 = new coninterpage();
          
          /*List<Account_Profile_Name__c> Lcs = new List<Account_Profile_Name__c>();
          Account_Profile_Name__c apn = new Account_Profile_Name__c();
          apn.Standard_Pages__c = true;
          lstContacts.add(apn);
          
          insert Lcs;*/
          
          
          /*Account_Profile_Name__c mc = Account_Profile_Name__c.getValues(userinfo.getProfileId());
    mc.Standard_Pages__c=true;
        update mc;*/
            try{conint2.inter();          }
            catch(exception e){
            
            }
         }
     }

 

sfdcfoxsfdcfox

You should always optimize before you test. Often times, optimization can remove the need for many tests.

 

Here is an optimized version of your code based on the parameters I believe you're trying to achieve:

 

public with sharing class coninterpage {
	apexpages.standardcontroller controller;
	
	public coninterpage(apexpages.standardcontroller controller) {
		this.controller = controller;
	}
	
	public pagereference inter() {
		pagereference ref;
		if(controller.getid() != null) {
			Account_Profile_Name__c mc = Account_Profile_Name__c.getValues(UserInfo.getProfileId());
			Contact record = [SELECT Id, AccountId, Account.RecordType.Name FROM Contact WHERE Id = :controller.getId()];
			if(mc.Standard_Pages__c) {
				if(record.account != null && record.account.recordtype != null && record.account.recordtype.name=='Person') {
					ref = new ApexPages.StandardController(new Account(Id=record.AccountId)).view();
				} else {
					ref = controller.view();
				}
				ref.getParameters().put('nooverride','1');
			} else {
				if(record.account != null && record.account.recordtype != null) {
					if(record.account.recordtype.name == 'Person') {
						ref = page.persontabdetailpage;
						ref.getparameters().put('id', record.accountid);
					} else {
						ref = page.conpagelayout;
						ref.getparameters().put('id', record.id);
					}
				} else {
					ref = page.conpagelayout;
					ref.getparameters().put('id', record.id);
				}
			}
		}
		if(ref != null) {
			ref.setRedirect(true);
		}
		return ref;
	}
}

From here, it will be easier to write your test method.

 

You should create two contacts, one person account type and one a corporate account type. From there, you need to construct two instances of the controller, one for each type. Next, set "standard pages" to false, then call inter() on each of the two controllers. Finally, set "standard pages" to true, and call inter() on both controllers again. You should get approximately 100% coverage.