+ Start a Discussion
fredkafredka 

Selecting a List value in Test

I am trying to write test code for a page/class that is used to select a value. A user launches the page from the opportunity object.  This page opens and displays a listing of products (custom object)  when the user selects a product from the list, fields from the record are passed to another custom object.

My issue is trying to select something from that list in my test.  I first thought I would assign the the ID so I used "controller.products[0].p.id = WellnessHP.id;"  however, I recieved an error that the list was null...  I did create a record for the same object in my test class which is named "WellnessHP"

Here is the code.. how can I "select" my test record?  THanks!!!! Fred
User-added image
Best Answer chosen by fredka
fredkafredka
By changing @IsTest to "@isTest(seealldata=true)" I was able to eliminate the error Thanks!

All Answers

Marty C.Marty C.
Hello, fredka, it may be helpful for you to post a copy of your controller code either directly here or using something like pastebin (http://pastebin.com/). The code you show in the screenshot looks fine, and it should work as long as the products property in your controller is being populated correctly. Assuming that you are indeed creating the Product2 record(s) in your test class that are intended to fill the products property, you should review your queries and test data to make sure that the records you created match the criteria for retrieval in your controller.

If your code touches Price Books, you may want to try adding annotating your test method with @isTest(SeeAllData = true) just in case that's a culprit behind the problem as well.
fredkafredka
Thanks so muc for responding!!  I don't use pricebooks and I also use custom objects for my proudcts.  Basically, I have a Horizon Productd object that holds my products.. The vf page displays a list of those products and then passes values to my opportunity_products__c object.  Here is the code:  Thanks again!!!!  Fred
public with sharing class AddWellnessOppProduct {
                    
 public Opportunity Opp {get; set; }
 public Wellness_Service__c oldWS { get; set; }
 public Opportunity_Products__c oldOppProd {get; set;}
 public Account a { get; set; }
 public List<ExtProduct> products { get; set; }
 public Boolean isWizard { get; set; }
 public Boolean canCreate { get; set; }
 public PageReference returnUrl { get; set; }
    
    public AddWellnessOppProduct () {
        if (apexPages.currentPage().getParameters().get('oid') != null) {
            Opp = [
                select id, Name, RecordType.Name, Account.Id, Account.Name, StageName
                from Opportunity
                where id = : apexPages.currentPage().getParameters().get('oid')];
                
            A = [select name, Health_Group_Number__c, Account_Manager_Servicing_health__c, Account_Manager_Servicing_health__r.id,
            Account_Manager_Servicing_health__r.name from Account where id = : Opp.Account.Id];  
        }
        else if (apexPages.currentPage().getParameters().get('pid') != null) {
            oldOppProd = [
                select Id, Name, 
                    opportunity__r.Account.Id, opportunity__r.Account.Name,
                    Horizon_Product__c, Horizon_Product__r.Name
                from Opportunity_Products__c
                where id = : apexPages.currentPage().getParameters().get('pid')];
                
            Opp = [
                select id, Name, RecordType.Name, Account.Id, Account.Name 
                from Opportunity
                where id = : oldOppProd.Opportunity__c];
                
            A = [select name, Health_group_Number__c, Account_Manager_Servicing_health__c, Account_Manager_Servicing_health__r.id,
            Account_Manager_Servicing_health__r.name from Account where id = : Opp.Account.Id]; 
        }
    
        returnUrl = new PageReference('/apex/WellnessOppWizard?oid=' + Opp.Id);
                                       
         List<RecordType> HPrtList = [
            select Id, Name
            from RecordType
            where sObjectType = 'Horizon_Products__c'
                and Name = 'Wellness'];            
                            
        List<RecordType> OppProdRTList = [
            select Id, Name
            from RecordType
            where sObjectType = 'Opportunity_Products__c' and Name = 'Wellness'];
            
        Map<String, String> wsrtMap = new Map<String, String>();
        
        for (RecordType rt : OppProdRTList) {
            wsrtMap.put(rt.Name, String.ValueOf(rt.Id));
        }
                
        if (HPrtList.size() == 1) {
            String newRtId = '';
            List<Horizon_Products__c> productList = new List<Horizon_Products__c>();
            if (HPrtList[0].Name == 'Wellness') {
                newRtId = wsrtMap.get('Wellness');
                   productList = [
                    select id, name, Plan_Design_Category__c, New_Product_Link_Wellness__c,
                        Change_Product_Link__c, product_category__c,
                        Standard__c
                    from Horizon_Products__c h 
                    WHERE RecordTypeId = : HPrtList[0].Id 
                        and Product_Status__c = 'Active' 
                    order by plan_design_category__c];
            } 
                    
            else {
                canCreate = false;
            }
            products = new List<ExtProduct>();
            for (Horizon_Products__c p : productList) {
                products.add(new ExtProduct(p, Opp, returnUrl.getUrl(), newRtId, HPrtList[0].Name, oldOppProd, a));
            }              
        }
        else {
            canCreate = false;
        }
             
    }
    
    public pageReference Cancel() {
        return returnUrl;
    }
    
    public class ExtProduct {
        public Horizon_Products__c p { get; set; }
        public String link { get; set; }

        public ExtProduct(Horizon_Products__c p, Opportunity Opp, String retUrl, String rtId, String rtName, Opportunity_Products__c oldOppProd, Account a) {
            this.p = p;
            if (oldOppProd == null) {
                this.link = p.New_Product_Link_Wellness__c;
                this.link = this.link.replace('<OppId>', string.valueOf(Opp.Id));
                this.link = this.link.replace('<OppName>', Opp.Name.replace(' ', '+'));
                this.link = this.link.replace('<HPId>', string.valueOf(p.Id));
                this.link = this.link.replace('<HPName>', p.Name.replace(' ', '+'));
                this.link = this.link.replace('<AId>', string.valueOf(a.Id));
                this.link = this.link.replace('<AName>', A.Name.replace(' ', '+'));
                this.link = this.link.replace('<HPPlanCategory>', p.plan_design_category__c.replace(' ', '+'));
                this.link = this.link.replace('<HPProdCategory>', p.product_category__c.replace(' ', '+'));
                this.link = this.link.replace('<rtId>', rtId);
                this.link = this.link.replace('<returnUrl>', EncodingUtil.urlEncode(retUrl, 'UTF-8'));
                
            }
            else {
                this.link = p.Change_Product_Link__c;
                this.link = this.link.replace('<oppProductId>', string.valueOf(oldOppProd.Id));
                this.link = this.link.replace('<returnUrl>', EncodingUtil.urlEncode(retUrl, 'UTF-8'));                                
            }
            
          
        }
    }
}

 
fredkafredka
And here is my test class.  I assume that I am not properly relating my test record to the list.. thanks again!!  Fred
@isTest 
private class AddWellnessOppProductTest {

 static testMethod void test1(){

	//create data
		// Retrieve Account record types
		 // Create a Horizion Product for BakedIn Wellness Service
        RecordType WRT = [
            select Id
            from Recordtype
            where sObjectType = 'Account'
            	and Name = 'World'];
    	        
        //Create a world account
        Account acct = new Account(
            name='test account32',
            recordtype = WRT,
            Type='Customer',
            Headquarter_State__c='NJ',
            Phone='1234567899',
            BillingStreet = '123 anystreet', 
            BillingCity = 'cinnaminson', 
            BillingState = 'NJ', 
            BillingPostalCode = '08077',
            Potential_NJ_Employees_Residents__c=100, 
            Potential_Total_Employees__c=150, 
            Account_Status__c = 'Active');
        insert acct;
		
        //create opportunity for NJ dental
         Opportunity WellnessOpp1 = new Opportunity(
            AccountId=acct.Id,
            Name='Wellness Opp 1',
            Stagename='Sales Proposal Prep',
            CloseDate= System.today().addmonths (1),
            region__C='NJ - Wellness',
            market_segment__C='Midsize',
            Date_Received_Quote_Request__c = system.today());
          insert WellnessOpp1;
        
        //create record type for 
         RecordType HPRT = [
            select Id
            from Recordtype
            where sObjectType = 'Horizon_Products__c'
            	and Name = 'Wellness'];
            	
        //create a wellness Horizon Products to be used for the opportunit product
   		 Horizon_Products__c WellnessHP = new Horizon_Products__c(
   		  Name = 'Flu Shots',
		  recordtype = HPRT,
   		  product_status__c = 'Active',
   		  line_of_business__c = 'Wellness',
   		  product_category__c = 'Wellness',
   		  plan_design__c = 'Flu Shots',
   		  plan_design_category__c = 'Miscellaneous Wellness Service');
   		 insert WellnessHP;
   		 
   		 //test the Horizon Product Name
   		 System.AssertEquals(WellnessHP.Name, 'Flu Shots');
        
    
        //create wellness opportunity product
         Opportunity_products__c WellnessProd1 = new Opportunity_Products__c(
   		  opportunity__c = WellnessOpp1.id,
   		  Name = 'WellnessTestProduct',
   		  status__c = 'Quoted',
   		  Horizon_Product__c = WellnessHP.id,
   		  estimated_contracts__c = 100,
   		  plan_design_category__c = 'Biometric Screenings',
   		  product_category__c = 'Wellness');
   		  insert WellnessProd1;
   		  
        //check Opp Product Opp ID
         System.assertEquals(wellnessProd1.opportunity__c, WellnessOpp1.id);
         
        
		//Use the PageReference Apex class to instantiate a page 
		  PageReference pageRef = Page.AddWellnessOppProduct; 
        //In this case, the Visualforce page named 'AddWellnessOppProduct' is the starting point of this test method.  
		  Test.setCurrentPage(pageRef); 
        //Instantiate and construct the controller class for the page and add the opp id to the url
        apexPages.currentPage().getParameters().put('oid',Wellnessopp1.id);
		AddWellnessOppProduct controller = new AddWellnessOppProduct();
		  
   //Test the cancel button
		//The .getURL will return the page url the Save() method returns. 
		 String CancelPage = controller.cancel().getUrl();
		 
		//Check that the cancel() method returns the proper URL. 
		 System.assertEquals('/apex/WellnessOppWizard?oid=' + Wellnessopp1.id, CancelPage); 

   //Test picking a product
   		//Select the product as if you picked it from the wizard
   		 controller.products[0].p = WellnessHP;
   		 //controller.products[0].p.id = WellnessHP.id;
   		//  System.assertEquals(controller.products[0].p.id, WellnessHP.id);
		  
		
	}

}

 
fredkafredka
By changing @IsTest to "@isTest(seealldata=true)" I was able to eliminate the error Thanks!
This was selected as the best answer
fredkafredka
The real issue was that I was assigning the recordtype incorrectly to one of my records.  Instead of the recordtypeid I was just assigning the record type