You need to sign in to do that
Don't have an account?
ckellie
Test Code Not Covering Class
The testr class below is not covering the class at all. Why and what do I need to change for the test class to cover the apex class?
Test code
@IsTest public class TestAddCustomerProductStage1create { /* This is a basic test which simulates the primary positive case for the save method in the AddCustomerProduct class. */ public static testMethod void myUnitTest() { PageReference pageRef = Page.CustomerProductStage1create; Test.setCurrentPageReference(pageRef); Customer_Product_Line_Item__c c = new Customer_Product_Line_Item__c(name='Apple'); insert c; System.assert (true, [select name from Customer_Product_Line_Item__c where name = 'Apple']); Account acc=new Account(Name='test', BillingStreet='test', BillingCity='NY', BillingState='test', BillingCountry='US', BillingPostalCode='test', ShippingStreet='test', ShippingCity='test', ShippingState='NY', ShippingCountry='US', ShippingPostalCode='test'); insert acc; Opportunity testOpp = new Opportunity(Name='TestOppty',closedate=date.parse('1/1/2222'), stagename = 'prospecting',AccountID=acc.id,Equip_Services_Options_to_be_quoted__c='test', Opportunity_Region__c='test'); insert testOpp; Customer_Product_Line_Item__c testcp = new Customer_Product_Line_Item__c(Name='Apple',Opportunity__c = testOpp.id); insert testcp; ApexPages.StandardController sc = new ApexPages.StandardController(testCP); String cpUrl = '/apex/customerproductstage1create?oppid=' + testOpp.id; System.Debug('++++++++++++'+cpUrl); PageReference ref = new PageReference(cpUrl); Test.setCurrentPage(ref); Test.startTest(); Customer_Product_Line_Item__c cl = new Customer_Product_Line_Item__c(name='Apple'); insert cl; test.stopTest(); /* Construct the standard controller for Customer Product. */ Account ac=new Account(Name='test', BillingStreet='test', BillingCity='NY', BillingState='test', BillingCountry='US', BillingPostalCode='test', ShippingStreet='test', ShippingCity='test', ShippingState='NY', ShippingCountry='US', ShippingPostalCode='test'); insert ac; Opportunity testpp = new Opportunity(Name='TestOppty',closedate=date.parse('1/1/2222'), stagename = 'prospecting',AccountID=acc.id,Equip_Services_Options_to_be_quoted__c='test', Opportunity_Region__c='test'); insert testpp; Customer_Product__c cp = new Customer_Product__c(Name='apple'); insert cp; Customer_Product_Line_Item__c cpli = new Customer_Product_Line_Item__c (Name= cp.name, Opportunity__c=testpp.id); insert cpli; ApexPages.StandardController con = new ApexPages.StandardController(new Customer_Product_Line_Item__c()); // Customer_Product_Line_Item__c cplitem = Testaddcustomerproductstage1create.setupTest(); PageReference p = new PageReference('/apex/CustomerProductStage1create'); p.getParameters().put('oppid', testpp.id); Test.setCurrentPage(p); ApexPages.currentPage().getParameters().put('oppid', testpp.id); } public static void setupTest() { /* Create an account */ Account a = new Account(); a.name = 'TEST'; Database.insert(a); /* Get the standard pricebook. There must be a standard pricebook already in the target org. */ Pricebook2 pb = [select name, isactive from Pricebook2 where IsStandard = true limit 1]; if(!pb.isactive) { pb.isactive = true; Database.update(pb); } /* Get a valid stage name */ OpportunityStage stage = [select MasterLabel from OpportunityStage limit 1]; /* Setup a basic opportunity */ Opportunity o = new Opportunity(); o.Name = 'TEST'; o.AccountId = a.id; o.CloseDate = Date.today(); o.StageName = stage.masterlabel; o.Pricebook2Id = pb.id; /* Create the opportunity */ Database.insert(o); Customer_Product_Line_Item__c cpli = new Customer_Product_Line_Item__c(); cpli.name = 'Apple'; cpli.Opportunity__c = o.id; Database.insert(cpli); PageReference pref = Page.CustomerProductStage1create; pref.getParameters().put('cpli.id',cpli.id); Test.setCurrentPage(pref); // return null; } }
Apex class
public class AddCustomerProductStage1create{ /* Constructor Function. The CustomerProduct id is captured in this function */ public Customer_Product_Line_Item__c cpl {get;set;} public Opportunity o; public AddCustomerProductStage1create(ApexPages.StandardController c) { o = [select id from Opportunity where id = :ApexPages.currentPage().getParameters().get('oppid')]; } public Opportunity getOpportunity() { return o; } /* Variable declarations */ public List<cCustomer> cpList {get; set;} // Wrapper class which stores customer product data and a boolean flag. public List<cCustomer> cpsel{get; set;} public ID oid {get; set;} String userinput; // cp Name String userinp; Public List<Customer_Product__c> results = new List<Customer_Product__c>(); // Customer_Product__c search results. Public List<Customer_Product__c> selproduct = new List<Customer_Product__c>(); // Customer_Product__c selectedproduct. /* End of Variable declarations */ /* Getter and setter methods for getting the user input ie. Customer_Product__c name from the UI */ public String getuserinput(){return userinput;} public void setuserinput(String userinp) { this.userinput=userinp; system.debug('*****SFDC-TEST-1**********'+userinput+'='+userinp); } public String getselproduct(){return null;} /*End of Getter and Setter methods */ /* Method to Search the Customer_Product__c database to return the query results */ public List<Customer_Product__c> cpsearch() { cpList = new List<cCustomer>(); for(Customer_Product__c c : [select id, name from Customer_Product__c where Name like :userinput+'%'order by Name asc]) { cpList.add(new cCustomer(c)); } system.debug('*****SFDC-TEST-2**********'+cpList); return null; } /* End of method */ /* Method for returning the Customer Product search results to the UI */ public List<cCustomer> getresults(){ return cpList; } public PageReference processSelected() { //We create a new list of Customer Products that we be populated only with Customer Products if they are selected List<Customer_Product__c> selectedProduct = new List<Customer_Product__c>(); //We will cycle through our list of cCustomer and will check to see if the selected property is set to true, //if it is we add the Customer Product to the selectedProduct list for(cCustomer cCon : getresults()) { if(cCon.selected == true) { selectedProduct.add(cCon.con); } } system.debug('*****SFDC-TEST-3**********'+selectedProduct); // Now we have our list of selected products and can perform any type of logic we want System.debug('These are the selected Products...'); for(Customer_Product__c cCon : SelectedProduct ) { system.debug(cCon); selproduct.add(cCon); } system.debug('*****SFDC-TEST-4**********'+selproduct.size()); cpsel = new List<cCustomer>(); for(Customer_Product__c p : [select name from Customer_Product__c where id =:selproduct]) { system.debug('*****SFDC-TEST-p**********'+p.name); cpsel.add(new cCustomer(p)); Customer_Product_Line_Item__c cpli = new Customer_Product_Line_Item__c(Name = cpsel[0].con.name, Opportunity__c = o.id); insert cpli; system.debug('*****test cpli.name**********'+cpli.name); } PageReference home = new PageReference('/' + o.id); home.setRedirect(true); return home; system.debug('*****SFDC-TEST-5**********'+cpsel.size()); return null; } /* Wrapper class to contain contact record and a boolean flag */ public class cCustomer{ public Customer_Product__c con {get; set;} public Boolean selected {get; set;} /*This is the contructor method. When we create a new cContact object we pass a Contact that is set to the con property. We also set the selected value to false*/ public cCustomer (Customer_Product__c c) { con = c; selected = false; } } /* end of Wrapper class */ }
Thank you
You probably need to pass parameters after creating the page reference
pageRef.getParameters().put('oppid', <variable holding id>);