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
salesforce_hoonigansalesforce_hoonigan 

Assistance with my Test Class

Hi Experts,

I am somewhat apex newb, I need a second eye with my test class and codes. As I am only getting 72% code coverage and I am not sure if I wrote it correctly.

Goal: Redirect the page layout to VF (Displaying 4 fields only) IF the Lead Owner is not equal to Current Running User

VF 1: 
<apex:page standardController="Lead" extensions="LeadViewRestriction" 
    action="{!nullValue(redir.url, urlFor($Action.Lead.View, Lead.id, null, true))}">
</apex:page>

VF 2:
<apex:page standardController="Lead"> 
    <apex:sectionheader title="{!$ObjectType.Lead.label} Detail" subtitle="{!Lead.Company}"/>
        <apex:pageblock mode="maindetail" title="{!$ObjectType.Lead.label} Detail">
            <apex:pageBlockSection title="Lead Information" columns="1"> 
                <apex:outputField title="Lead Owner" value="{!Lead.OwnerId}"/> 
                <apex:outputField title="Company Name" value="{!Lead.Company}"/> 
                <apex:outputField title="Status" value="{!Lead.Lead_Status_NEW__c}"/> <BR></BR>
            </apex:pageBlockSection> 
        </apex:pageBlock> 
</apex:page>

Controller:
public class LeadViewRestriction {

    public LeadViewRestriction (ApexPages.StandardController controller) {
        this.controller = controller;
    }

    public PageReference getRedir() {
        Lead l = [Select id, OwnerId From Lead Where Id =: ApexPages.currentPage().getParameters().get('id')];
        User u = [Select Id, Profile.Name from User where Id =: UserInfo.getUserId()];
        
        PageReference newPage;

        if (u.Profile.Name == 'VL - Sales Agent' && l.OwnerId != u.Id) {
            newPage = Page.LeadBasicView;
        } else {
            return null;
        }


        newPage.getParameters().put('id', l.Id);
        return newPage.setRedirect(true);

    }

    private final ApexPages.StandardController controller;

}

TEST CLASS:
 
@isTest
private class TestLeadViewRestriction {
@isTest static void testsLeadView(){
    Lead l = new Lead();
    l.Company= 'Test';
    l.LastName = 'Doe';
    l.Lead_Status_NEW__c = 'New';
    insert l;

    Test.setCurrentPageReference(new PageReference('Page.LeadBasicView'));
    System.currentPageReference().getParameters().put('Id', l.Id);

    ApexPages.StandardController sc = new ApexPages.standardController(l);
    LeadViewRestriction controller = new LeadViewRestriction (sc);       

    controller.getRedir();


}
}

Any assistance with arranging the framework is greatly appreciated.

Thank you.

 
UC InnovationUC Innovation

You will need to create a test profile with the name "VL - Sales Agent' and a test user assigned to that profile. Then you can run the test as that user.

Here's some documentation on the runAs method.

http://​https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_tools_runas.htm

Good luck! Let me know if that helps!

salesforce_hoonigansalesforce_hoonigan

Hi,

I am getting red flags in the Developer Console in Line 14,20,21 of Apex Class.

Thank you.

Amit Chaudhary 8Amit Chaudhary 8
PLease try below code
@isTest
private class TestLeadViewRestriction 
{
	@isTest 
	static void testsLeadView()
	{
		Lead l = new Lead();
		l.Company= 'Test';
		l.LastName = 'Doe';
		l.Lead_Status_NEW__c = 'New';
		insert l;

		Test.setCurrentPageReference(new PageReference('Page.LeadBasicView'));
		System.currentPageReference().getParameters().put('Id', l.Id);

		ApexPages.StandardController sc = new ApexPages.standardController(l);
		LeadViewRestriction controller = new LeadViewRestriction (sc);       

		controller.getRedir();
	}

	@isTest 
	static void testsLeadView1()
	{
		Lead l = new Lead();
		l.Company= 'Test';
		l.LastName = 'Doe';
		l.Lead_Status_NEW__c = 'New';
		insert l;

		Profile p = [SELECT Id FROM Profile WHERE Name='VL - Sales Agent']; 
		
		User u = new User(Alias = 'standt', Email='standarduser@testorg.com', 
			EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
			LocaleSidKey='en_US', ProfileId = p.Id, 
			TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg.com');
		insert u;
		
		System.runAs(u) 
		{
			Test.setCurrentPageReference(new PageReference('Page.LeadBasicView'));
			System.currentPageReference().getParameters().put('Id', l.Id);
			ApexPages.StandardController sc = new ApexPages.standardController(l);
			LeadViewRestriction controller = new LeadViewRestriction (sc);       
			controller.getRedir();
		}
	}
}
Let us know if this will help you
 
salesforce_hoonigansalesforce_hoonigan
Hi @Amit,

I am getting an error:

Pass/Fail: Fail
Error Message: System.QueryException: List has no rows for assignment to SObject
Stack Trace: Class.LeadViewRestriction.getRedir: line 8, column 1
                    Class.TestLeadViewRestriction.testsLeadView1: line 45, column 1
salesforce_hoonigansalesforce_hoonigan
Hi @Amit,

I appreciate the help. I am still getting an error:

Pass/Fail: Fail
Error Message: System.QueryException: List has no rows for assignment to SObject
Stack Trace: Class.LeadViewRestriction.getRedir: line 8, column 1
                    Class.TestLeadViewRestriction.testsLeadView1: line 48, column 1
Amit Chaudhary 8Amit Chaudhary 8
Please try below
@isTest
private class TestLeadViewRestriction 
{
	@isTest 
	static void testsLeadView()
	{
		Lead l = new Lead();
		l.Company= 'Test';
		l.LastName = 'Doe';
		l.Lead_Status_NEW__c = 'New';
		insert l;

		Test.setCurrentPageReference(new PageReference('Page.LeadBasicView'));
		System.currentPageReference().getParameters().put('id', l.Id);

		ApexPages.StandardController sc = new ApexPages.standardController(l);
		LeadViewRestriction controller = new LeadViewRestriction (sc);       

		controller.getRedir();
	}

	@isTest 
	static void testsLeadView1()
	{
		ID userID = UserInfo.getUserId() ; 
		
		Profile p = [SELECT Id FROM Profile WHERE Name='VL - Sales Agent']; 
		
		User u = new User(Alias = 'standt', Email='standarduser@testorg.com', 
			EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
			LocaleSidKey='en_US', ProfileId = p.Id, 
			TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg.com');
		insert u;
		
		System.runAs(u) 
		{
			Lead l = new Lead();
			l.Company= 'Test';
			l.LastName = 'Doe';
			l.Lead_Status_NEW__c = 'New';
			l.ownerid = userID;
			insert l;
		
			Test.setCurrentPageReference(new PageReference('Page.LeadBasicView'));
			System.currentPageReference().getParameters().put('id', l.Id);
			ApexPages.StandardController sc = new ApexPages.standardController(l);
			LeadViewRestriction controller = new LeadViewRestriction (sc);       
			controller.getRedir();
		}
	}
}

 
salesforce_hoonigansalesforce_hoonigan
Hi @Amit,

Weird. Still getting the same error as above.

Thank you.