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
Joanne ButterfieldJoanne Butterfield 

Help with Controller Extension test method

Hi All,
I have been given this great  code; however I am not a developer and having difficulties on writing the test code.
I have googled and search many test codes, but am lost and stuck. Any help would be much appreciated.
Thank you in advance!

Apex Class
01	<apex:page standardController="Case" extensions="OppProductsOnCaseController" title="Related Opportunity Products">
02	  <apex:form id="oppProdForm">
03	      <apex:pageBlock id="prodPB" title="Opportunity Product Details">
04	          <apex:pageBlockTable id="pbTable" value="{!oppLineItem}" var="oli">
05	              <apex:column headerValue="Product Name" value="{!oli.PriceBookEntry.Product2.Name}" />
06	              <apex:column headerValue="Quantity" value="{!oli.Quantity}"/>             
07	          </apex:pageBlockTable>     
08	      </apex:pageBlock>
09	  </apex:form>
10	</apex:page>

VF Page
01	public class OppProductsOnCaseController{
02	     
03	    // declatre variables to hold the current case record
04	    public Case inContextCase {get; set;}
05	    // decalre variable to hold the list of oli fields for the related opportunity
06	    public List<OpportunityLineItem> oppLineItem {get; set;}
07	     
08	    // constructor for the class
09	    public OppProductsOnCaseController(ApexPages.StandardController stdCont){
10	        // instantiate the variables decalred
11	        inContextCase = new Case();
12	        oppLineItem = new List<OpportunityLineItem>();
13	         
14	        // get the in context record from the passed in argument in the constructor
15	        inContextCase = (Case) stdCont.getRecord();
16	        // query the necessary fields from the case object like the related opportunity field value
17	        inContextCase = [SELECT Id, Related_Opportunity__c FROM Case WHERE Id = :inContextCase.Id];
18	         
19	        // if the case is related to an opportunity then using the opportunity id query and fetch the opportunity line items
20	        if(inContextCase.Related_Opportunity__c != null){
21	            // add or delete fields from the query as required
22	            oppLineItem = [SELECT Id, ListPrice, OpportunityId, PriceBookEntryId, PriceBookEntry.Product2.Name, Quantity, UnitPrice, TotalPrice FROM OpportunityLineItem
23	                            WHERE OpportunityId = :inContextCase.Related_Opportunity__c];
24	            if(oppLineItem != null && oppLineItem.size() > 0){
25	                // do further operations with opp line items if required.
26	                // if not then delete the if condition
27	            }
28	        }
29	         
30	    }
31	}


 
Best Answer chosen by Joanne Butterfield
cmoylecmoyle
This should get you started. It definitley needs some additional information around creating the dummy objects (I didn't know what fields were required off hand). You should also add any asserts for any expectations you have.
 
@ isTest
public class TestCase {

	static testMethod void testController() {
		/**
		 *	Create dummy objects for testing. Might need to add missing required fields
		 */
		Opportunity o = new Opportunity(
			Name = 'My Opp'
		);
		insert o;
		
		OpportunityLineItem oli = new OpportunityLineItem(
			Name = 'MyLineItem',
			OpportunityId = o.Id,
			Quantity = 5,
			UnitPrice = 5
		);
		insert oli;
		
		Case c = new Case(
			Name = 'My Case',
			Related_Opportunity__c = o.Id	
		); 

		insert c;

		Test.startTest();
			ApexPages.standardController sc = new ApexPages.standardController(c);
			OppProductsOnCaseController controller = new OppProductsOnCaseController(sc);
			System.assertNotEquals(null, controller.inContextCase);
			System.assertEquals(1, controller.oppLineItem.size());
			
		Test.stopTest();
	}

}

 

All Answers

cmoylecmoyle
This should get you started. It definitley needs some additional information around creating the dummy objects (I didn't know what fields were required off hand). You should also add any asserts for any expectations you have.
 
@ isTest
public class TestCase {

	static testMethod void testController() {
		/**
		 *	Create dummy objects for testing. Might need to add missing required fields
		 */
		Opportunity o = new Opportunity(
			Name = 'My Opp'
		);
		insert o;
		
		OpportunityLineItem oli = new OpportunityLineItem(
			Name = 'MyLineItem',
			OpportunityId = o.Id,
			Quantity = 5,
			UnitPrice = 5
		);
		insert oli;
		
		Case c = new Case(
			Name = 'My Case',
			Related_Opportunity__c = o.Id	
		); 

		insert c;

		Test.startTest();
			ApexPages.standardController sc = new ApexPages.standardController(c);
			OppProductsOnCaseController controller = new OppProductsOnCaseController(sc);
			System.assertNotEquals(null, controller.inContextCase);
			System.assertEquals(1, controller.oppLineItem.size());
			
		Test.stopTest();
	}

}

 
This was selected as the best answer
Joanne ButterfieldJoanne Butterfield
Hi Cmoyle,
This is Great, Thank you!
I'm getting errors on the pricebook ID, System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: PricebookEntryId, unknown (versions 3.0 and higher must specify pricebook entry id, others must specify product id): [PricebookEntryId, unknown]
I tried adding PricebookEntryID = pb.ID after the unit price line item but still getting errors. Variable does not exist: pb.ID
Any advice on the price book ID?
Thank you again for your help.
 
Joanne ButterfieldJoanne Butterfield
Hi Cmoyle,
I got it working, Thank you!