+ Start a Discussion
Jordan FrankJordan Frank 

Custom Controller Testing

Hey Guys, 

Very new to the apex/custom controller world, in fact this is my first one. I am building a custom dashboard filled with VF pages that call a custom controller. The controller works as expected but I cannot deploy and more change sets to production because my code coverage is too low. I did not realize I have to build a test class in order to keep that code coverage from returning 0... Here is my custom controller, can someone help me understand how I would write a test class for this? Thanks!
public class retrieveBS {
    public String getEngineerName() {
        return 'Bob Smith';
     }
    
    public List<Opportunity> getOpportunities() {
        return [Select Opportunity.Account.name, StageName, Name, Job_Num__c, Project_Sponsor__c, Project_Manager1__c, MHE_Engineer__c, MHE_Engineer2__c, SDS_Engineer__c, Software_Engineer__c, 
                SH_Assigned__c FROM Opportunity
                WHERE (Project_Sponsor__c = 'Bob Smith' OR Project_Manager1__c = 'Bob Smith' OR 
                MHE_Engineer__c = 'Bob Smith' OR MHE_Engineer2__c = 'Bob Smith' OR SDS_Engineer__c = 'Bob Smith' 
                OR Software_Engineer__c = 'Bob Smith') AND (StageName = 'Application Engineering' OR 
                StageName = 'Commitment to Buy' OR StageName = 'Closed Won') AND (Execution_Status__c = 'Active' OR
                Execution_Status__c = 'Implementation' OR (Proposal_Status__c = 'Active' AND RecordTypeID = '0121a000000RLqA'))
                limit 10];
     }
}

 
Best Answer chosen by Jordan Frank
Amit Chaudhary 8Amit Chaudhary 8
Try below code
@isTest
public class retrieveBSTest
{
		public static testmethod void test1()
		{
				/*
				Opportunity op=new Opportunity();
				op.Project_Sponsor__c = 'Bob Smith';
				op.StageName = 'Application Engineering';
				op.Execution_Status__c = 'Active';
				op.RecordTypeID = '0121a000000RLqA';
				insert op;
			    */

			   retrieveBS obj=new retrieveBS();
			   System.assertEquals('Bob Smith',obj.getEngineerName());

			   List<Opportunity> oplist=obj.getOpportunities();
		}
}

Let us know if this will help you
 

All Answers

Jordan FrankJordan Frank
This is definitely the right track, however, when I run the test it gives me an error saying this id isn't valid for user? But it's using a different id than specified. 
Amit Chaudhary 8Amit Chaudhary 8
Try below code
@isTest
public class retrieveBSTest
{
		public static testmethod void test1()
		{
				/*
				Opportunity op=new Opportunity();
				op.Project_Sponsor__c = 'Bob Smith';
				op.StageName = 'Application Engineering';
				op.Execution_Status__c = 'Active';
				op.RecordTypeID = '0121a000000RLqA';
				insert op;
			    */

			   retrieveBS obj=new retrieveBS();
			   System.assertEquals('Bob Smith',obj.getEngineerName());

			   List<Opportunity> oplist=obj.getOpportunities();
		}
}

Let us know if this will help you
 
This was selected as the best answer
Jordan FrankJordan Frank
Even when I comment out the above code, I still get the error saying Invalid Cross Reference Key, RecordTypeID. ID is not valid for this user, and then the recordtypeid it lists is actually a record type in my production org. It is not in my sandbox, any ideas? 
Amit Chaudhary 8Amit Chaudhary 8
try to remove hard coding like below and then try above test class
public class retrieveBS {
    public String getEngineerName() {
        return 'Bob Smith';
     }
    
    public List<Opportunity> getOpportunities() 
	{
		Id OppRecordTypeId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('RERCORDTYPENAME').getRecordTypeId();

		
        return [Select Opportunity.Account.name, StageName, Name, Job_Num__c, Project_Sponsor__c, Project_Manager1__c, MHE_Engineer__c, MHE_Engineer2__c, SDS_Engineer__c, Software_Engineer__c, 
                SH_Assigned__c FROM Opportunity
                WHERE (Project_Sponsor__c = 'Bob Smith' OR Project_Manager1__c = 'Bob Smith' OR 
                MHE_Engineer__c = 'Bob Smith' OR MHE_Engineer2__c = 'Bob Smith' OR SDS_Engineer__c = 'Bob Smith' 
                OR Software_Engineer__c = 'Bob Smith') AND (StageName = 'Application Engineering' OR 
                StageName = 'Commitment to Buy' OR StageName = 'Closed Won') AND (Execution_Status__c = 'Active' OR
                Execution_Status__c = 'Implementation' OR (Proposal_Status__c = 'Active' AND RecordTypeID = :OppRecordTypeId ))
                limit 10];
     }
}

 
Jordan FrankJordan Frank
Same error... User-added image
Jordan FrankJordan Frank
Amit, your solution got me to what I needed. I did not put the changes in the main retrieve class but I did keep out some of the code in the test class which worked. Here is the final test class. 
@isTest
public class retrieveBSTest { 
static testMethod void test1() {
date testDate = date.newInstance(2017,2,10);
Opportunity op = new Opportunity();
op.Project_Sponsor__c = 'Bob Smith';
op.StageName = 'Application Engineering';
op.Proposal_Status__c  = 'Active';
op.Name = 'Testing Engineer';
op.CloseDate = testDate;
insert op;

retrieveBS obj = new retrieveBS();
System.assertEquals('Bob Smith', obj.getEngineerName());
List<Opportunity> oplist = obj.getOpportunities();
}
}