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
chandu kumarchandu kumar 

test class for below code.please help me to findout.

public class newRFPController {
    public Product2 product;
    public Question__c question {get;set;}
    public Carrier_Response_Status__c carrier {get;set;}
    public Contact contact {get;set;}
    public Question_Product__c questionproduct {get;set;}
    public string UserSelectVAlue {set;get;}
    public string UserSelectOptInQ {set;get;}
    public String UserSelectFamily {get;set;}
    public string UserSelectCarrier {set;get;}     
    public boolean OptInQuestion {set;get;}
    public List<Product2> newprd {set;get;}
    public List<Contact> newcontact {set;get;}
    public List<Carrier_Response_Status__c> newcarrier {set;get;}
    private string selectedtab;
    public Set<id> productIds;
    public Id RFPId;
    public list<RFP_Product__c> newRFPProdList {set;get;}
    public list<RFP_Product__c> delRFPProdList;
    public list<Carrier_Response_Status__c> delcarrier;
    public List<CCWRow> tRecords{get;set;}
    public List<RFP_Question__c> selectedQuestions{get;set;}
    public List<RFP_Question__c> selectedOptInQuestions{get;set;}
    public List<Question_Product__c> editQuestions {get;set;}
    public String sDeveloperName;
    public String RFPname {set;get;}
    private string exceptionMessages = ''; 
    public list<String> OptInQ {get;set;}
    set<String> setOptInQ = new set<String>();
    public ID targetQuestionId {set;get;}
    private String [] m_lstVals = new String[]{};
        public void setValues (String [] lstVals) { 
            this.m_lstVals = lstVals;
            system.debug('m_lstVals1'+m_lstVals);
        } 
    public String [] getValues () { 
        system.debug('m_lstVals1'+m_lstVals);
        return m_lstVals; 
    } 
    public newRFPController(){
        this.newprd = new List<Product2>();
        this.newcarrier = new List<Carrier_Response_Status__c>();
        this.delcarrier = new List<Carrier_Response_Status__c>();
        this.carrier = new Carrier_Response_Status__c();
        this.contact = new Contact();
        this.productIds = new Set<id>();
        this.selectedTab = 'tab1';
        this.OptInQuestion = false;
        this.RFPId = ApexPages.currentPage().getParameters().get('id');
        this.newRFPProdList = new list<RFP_Product__c>();
        this.delRFPProdList = new list<RFP_Product__c>();
        this.tRecords = new List<CCWRow>();
        this.selectedQuestions = new List<RFP_Question__c>();
        this.selectedOptInQuestions = new List<RFP_Question__c>();
        this.OptInQ = new list<String>();
        //set initial page
        this.page = 1;
        pageSize = 10;
        displayPopup = false;
        for (RFP_Product__c existingprd: [select id, Product__r.id,Productname__c, Description__c, Productcode__c, ProductFamily__c from RFP_Product__c where Product__r.type__c = 'Plan level' and RFP__c=:RFPId]){
            Product2 addedprd = new Product2();
            addedprd.id = existingprd.Product__r.id;
            newprd.add(addedprd);
            productIds.add(addedprd.id);
            newRFPProdList.add(existingprd);
        }
        for(Question__c q: [select SubGroup__c from Question__c where Opt_in_question__c = true and id in (select Question__c from rfp_question__c where RFP__c=:RFPId)]){            
            if (q != null){
                setOptInQ.add(q.SubGroup__c);                
            }
        }
        OptInQ.addAll(setOptInQ);
        for (Carrier_Response_Status__c existingcar: [select id, name,Key_Contact__c, Locked__c, Status__c from Carrier_Response_Status__c where RFP__c=:RFPId ]){
            newcarrier.add(existingcar);
        }
        for (rfp__c r: [select name from rfp__c where id=: RFPId Limit 1]){
            RFPName = r.name;
        }
    }
    public PageReference Add (){
        boolean duplicate = false;
        if(UserSelectVAlue != null){
            Product2 addedprd = [select id,name, Description, ProductCode, Family from Product2 where name=: UserSelectVAlue];
            addedprd.Description = addedprd.name;
            for (Product2 prd: newprd){
                if(prd.id == addedprd.Id){
                    duplicate = true;
                    break;
                }   
            }
            if (!duplicate){
                newprd.add(addedprd);
                productIds.add(addedprd.id);
                RFP_Product__c newRFPProd = new RFP_Product__c();
                newRFPProd.Product__c = addedprd.id;
                newRFPProd.RFP__c = RFPId;
                newRFPProd.Description__c = addedprd.Description;
                newRFPProd.Productname__c = addedprd.name;
                newRFPProd.Productcode__c = addedprd.ProductCode;
                newRFPProd.ProductFamily__c = addedprd.Family;
                newRFPProdList.add(newRFPProd);
                Save();
                addquestions();
            }
        }
        return null;
    }
    public boolean displayPopup {get; set;}
    public rfp_question__c newManualQ {get;set;}
    
    public PageReference closePopup()
    {
        displayPopup = false;
        return null;
    }
    public PageReference AddnewManualQ(){
        displayPopup = false;
        if(newManualQ != null)
            upsert newManualQ;
        reOrder();
        getQuestions();
        return null;
    }
    public PageReference AddManualQuestions (){
        newManualQ = new rfp_question__c();
        displayPopup = true;
        ID ManualQuestionId;
        for (question__c rid: [select id from question__c where name ='Manual question' limit 1]){
            ManualQuestionId = rid.id;
        }
        newManualQ.Question__c = ManualQuestionId;
        newManualQ.RFP__c = RFPId;
        newManualQ.Question_Text__c = '';
        newManualQ.Type__c = '';
        newManualQ.Subgroup__c = '';
        newManualQ.ProductName__c = '';
        //newManualQ.Chapter__c = '';
        return null;
    }
    public PageReference AddOptInQuestions (){
        List<rfp_question__c> rqlist = new List<rfp_question__c>();
        if(UserSelectOptInQ!= null){
            if (!setOptInQ.contains(UserSelectOptInQ)){
                for(Question_Product__c c: [SELECT Question__r.Id, Question__c, Product__c, Question__r.Question_Text__c, Question__r.type__c,Question__r.Chapter__c, Question__r.Subgroup__c, Product__r.name 
                                            FROM Question_Product__c WHERE Question__r.Opt_in_question__c =:true and Question__r.Subgroup__c=:UserSelectOptInQ limit 100]){
                                                RFP_Question__c rq = new RFP_Question__c();
                                                rq.Question__c = c.Question__r.id;
                                                rq.Question_Text__c = c.Question__r.Question_Text__c;
                                                rq.RFP__c = RFPId;
                                                rq.Product__c = c.Product__c;
                                                //rq.Unique_ID__c = RFPId + '-' + c.Question__r.ID;
                                                rq.Type__c = c.Question__r.Type__c;
                                                rq.Subgroup__c = c.Question__r.Subgroup__c;
                                                rq.Chapter__c = c.Question__r.Chapter__c;
                                                rq.ProductName__c = c.Product__r.name;
                                                rq.status__C = 'Active';
                                                selectedOptInQuestions.add(rq);
                                            }
                setOptInQ.add(UserSelectOptInQ);
                OptInQ.add(UserSelectOptInQ);
            }
            if(selectedOptInQuestions.size()>0)
                upsert selectedOptInQuestions;
        }
                    Integer i;
            for(rfp_question__c ri: [select no__c  from rfp_question__c where Status__c != 'Removed' and RFP__c=:RFPId and no__c != null order by no__c desc limit 1]){
                i = Integer.valueOf(ri.no__c);
            }
            if (i == null)
                i = 0;
            List<rfp_question__c> rqlist1 = new List<rfp_question__c>();
            for(rfp_question__c rq: [select id, no__c, Question__r.Subgroup__c, Question__r.Type__c, Product__r.name from rfp_question__c 
                                     where RFP__c=:RFPId and no__c = null]){
                                         i++;
                                         rq.no__c = i;
                                         rqlist1.add(rq);
                                     }
            if(rqlist1.size()>0)
                upsert rqlist1;
        return null;
    }
    public PageReference reOrder(){
            Integer i;
            boolean reOrder = false;
            List<rfp_question__c> rqlist1 = new List<rfp_question__c>([select id, no__c from rfp_question__c rq where Status__c != 'Removed' and RFP__c=:RFPId order by no__c,Product__c ]);
            i = rqlist1.size();
            //system.debug('rqlist1size'+i);
        for (Integer j= 1;j<= i; j++){
            //system.debug('jsize'+j);
            //system.debug('isize'+Integer.valueof(rqlist1.get(j-1).no__c));
            if (j != Integer.valueof(rqlist1.get(j-1).no__c)){
                reOrder = true;
                break;
            }   
        }
        if (reOrder){
            i =0;
            List<rfp_question__c> rqlist2 = new List<rfp_question__c>();
            for(rfp_question__c rq: rqlist1){
                                         i++;
                                         rq.no__c = i;
                                        rqlist2.add(rq);
                                     }
            if(rqlist2.size()>0)
                upsert rqlist2;
        }
        getQuestions();
        return null;
    }
    public PageReference Del (){
        Integer dmlrows = Limits.getLimitDMLRows();
        Integer curdmlrows = Limits.getDMLRows(); 
        Integer remainingdmlrows = dmlrows - curdmlrows;
        
        Integer indexVal = Integer.valueof(system.currentpagereference().getparameters().get('index'));  
        
        if (newRFPProdList.get(indexVal - 1).id!=null)
            delRFPProdList.add(newRFPProdList.get(indexVal - 1));
        
        Id prodid = newRFPProdList.get(indexVal - 1).Product__c;  
        system.debug('selectedprodid'+prodid);
        try{
        delete [select ID from rfp_question__c where rfp__c = :rfpID and product__c =: prodid];
        remainingdmlrows = dmlrows - curdmlrows;
        
        newprd.remove(indexVal - 1);
        newRFPProdList.remove(indexVal - 1);
        
        if(delRFPProdList.size()>0)
            delete delRFPProdList;
        delRFPProdList.clear();
        }
        catch (Exception e) {
          String  message  = '[ PACRES ] Exception: ' + e.getMessage() + '; line: ' + e.getLineNumber() ;
            //ApexPages.addMessage(message);
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, '[ PACRES ] Exception: ' + e.getMessage() ));
            exceptionMessages += message + '\n\n';
        }
        return null;
    }
    public PageReference DelOpInQ (){
        Integer indexVal = Integer.valueof(system.currentpagereference().getparameters().get('index1'));
        String q = OptInQ.get(indexVal - 1);
        delete [select ID from rfp_question__c where rfp__c = :rfpID and SubGroup__c =: q];
        OptInQ.remove(indexVal - 1);
        return null;
    }
    public PageReference Delcar (){
        Integer indexVal = Integer.valueof(system.currentpagereference().getparameters().get('indexcar'));
        if (newcarrier.get(indexVal - 1).id!=null)
            delcarrier.add(newcarrier.get(indexVal - 1));
        newcarrier.remove(indexVal - 1);
        return null;
    }
    public PageReference Save (){
        if(newRFPProdList.size()>0)
            upsert newRFPProdList;
        if(delRFPProdList.size()>0)
            delete delRFPProdList;
        return null;
    }
    public PageReference Savecar (){
        if(newcarrier.size()>0)
            upsert newcarrier;
        if(delcarrier.size()>0)
            delete delcarrier;
        return null;
    }
    public PageReference SaveQuestions (){
        selectedQuestions.clear();
        for (CCWRow c: tRecords){
            if(c.IsSelected == true){
                //c.tContact.Status__c = 'Active';
                selectedQuestions.add(c.tContact);    
            }
        }
        if(selectedQuestions.size()>0)
            upsert selectedQuestions;
        reOrder();
        getQuestions();
        return null;
    }
    public PageReference quickSaveQ (){
        selectedQuestions.clear();
        for (CCWRow c: tRecords){
            //if(c.IsSelected == true){
                selectedQuestions.add(c.tContact);
              //  break;
            //}
        }
        if(selectedQuestions.size()>0)
            upsert selectedQuestions;
                reOrder();
        getQuestions();
        return null;
    }
    public PageReference removeselected(){
        selectedQuestions.clear();
        for (CCWRow c: tRecords){
            if(c.IsSelected == true){
                c.tContact.Status__c = 'Removed';
                selectedQuestions.add(c.tContact);    
            }
        }
        if(selectedQuestions.size()>0)
            upsert selectedQuestions;
        reOrder();
        getQuestions();
        return null;
    }
    public List<SelectOption> getListVal()
    { 
        List<SelectOption> listValues = new List<SelectOption>();
        listValues.add(new SelectOption('','--None--'));
        String prodfamily;
        if (UserSelectFamily != null){
            for(Product2 pname: [select name from Product2 where family=:UserSelectFamily and type__c = 'Plan level' limit 200])
            {
                listValues.add(new SelectOption(pname.name,pname.name));
            }
            
        }else
        {
            for(Product2 pname: [select name from Product2 where type__c = 'Plan level' limit 200])
            {
                listValues.add(new SelectOption(pname.name,pname.name));
            }
        }
        return listValues;
    }
    public PageReference updfamily(){
        UserSelectFamily = product.family;
        return null;
    }
    public List<selectOption> getmultiListVal() {
        List<SelectOption> listValues = new List<SelectOption>();
        set<selectOption> setValues = new set<selectOption>();
        listValues.add(new SelectOption('','--None--'));
        for(Question__c qobject: [select Subgroup__c from Question__c WHERE Opt_in_question__c = true AND Subgroup__c != null AND ID in (select question__c from question_product__c where Product__c in :productIDs) limit 500])
        {          
            setValues.add(new SelectOption(qobject.Subgroup__c,qobject.Subgroup__c));
        }
        listValues.addAll(setValues);
        return listValues;
    }
    public List<selectOption> getpagesizeVal() {
        List<SelectOption> listValues = new List<SelectOption>();
        listValues.add(new SelectOption('10','10'));
        listValues.add(new SelectOption('20','20'));
        listValues.add(new SelectOption('30','30'));
        listValues.add(new SelectOption('50','50'));
        listValues.add(new SelectOption('100','100'));
        return listValues;
    }
    public PageReference Addcarrier(){
        if(carrier.Key_Contact__c!=null)
            UserSelectCarrier = carrier.Key_Contact__c;     
        system.debug('CarrierID'+UserSelectCarrier);
        iF (UserSelectCarrier!=NULL){
            newcontact = new List<Contact>();
            Contact addedcontact = [select id, name, Account.name from Contact where id=: UserSelectCarrier];
            Carrier_Response_Status__c addedcarrier = new Carrier_Response_Status__c();
            addedcarrier.name = addedcontact.account.name;
            addedcarrier.Key_Contact__c = addedcontact.id;
            addedcarrier.Status__c = 'Open';
            addedcarrier.RFP__c = RFPId;
            newcarrier.add(addedcarrier);
        }
        return null;
    }
    public PageReference exitpage(){
        PageReference returnPage = new PageReference('/'+RFPId); 
        returnPage.setRedirect(true);
        return returnPage;
    }
    public Product2 getProduct(){
        if(product == null) product = new Product2();
        return product;
    }
    public boolean getistab1disabled() {
        return selectedTab <> 'tab1';
    }
    
    public boolean getistab2disabled() {
        return selectedTab <> 'tab2';
    }
    public boolean getistab3disabled() {
        return selectedTab <> 'tab3';
    }
    
    public boolean getistab4disabled() {
        return selectedTab <> 'tab4';
    }
    public boolean getistab5disabled() {
        return selectedTab <> 'tab5';
    }
  
Amit Chaudhary 8Amit Chaudhary 8
Please check below post to learn about test classes
1) http://amitsalesforce.blogspot.in/search/label/Test%20Class
2) http://amitsalesforce.blogspot.in/2015/06/best-practice-for-test-classes-sample.html

Sample test class for your
 
@isTest 
public class newRFPControllerTest 
{
	static testMethod void testMethod1() 
	{
	
		Question__c qun = new Question__c();
		// add all required field
		insert qun;
		
		RFP_Product__c rfpProd = new RFP_Product__c();
		//rfpProd.RFP__c = // add id here
		//rfpProd.Product__c = // add id here
		// add all required field here
		insert rfpProd;
		
		Test.StartTest(); 
			
			PageReference pageRef = Page.AccountPlan; // Add your VF page Name here
			pageRef.getParameters().put('id', String.valueOf(rfpProd.Id));
			Test.setCurrentPage(pageRef);
			
			newRFPController obj = new newRFPController();
			obj.closePopup();
			obj.AddnewManualQ();
			obj.AddManualQuestions();
			obj.AddOptInQuestions();
			obj.reOrder();
			
			obj.UserSelectVAlue ='ProductName';
			obj.Add();
			
			obj.Del();
			obj.DelOpInQ();
			obj.Delcar();
			obj.Save();
			obj.Savecar();
			obj.SaveQuestions();
			obj.quickSaveQ();
			obj.removeselected();
			obj.updfamily();
			obj.exitpage();
			Product2 p =obj.getProduct();
			// call all method.
		
		Test.StopTest();
	}
}


Please follow below salesforce Best Practice for Test Classes :-

1. Test class must start with @isTest annotation if class class version is more than 25
2. Test environment support @testVisible , @testSetUp as well
3. Unit test is to test particular piece of code working properly or not .
4. Unit test method takes no argument ,commit no data to database ,send no email ,flagged with testMethod keyword .
5. To deploy to production at-least 75% code coverage is required 
6. System.debug statement are not counted as a part of apex code limit.
7. Test method and test classes are not counted as a part of code limit
9. We should not focus on the  percentage of code coverage ,we should make sure that every use case should covered including positive, negative,bulk and single record .
Single Action -To verify that the the single record produces the correct an expected result .
Bulk action -Any apex record trigger ,class or extension must be invoked for 1-200 records .
Positive behavior : Test every expected behavior occurs through every expected permutation , i,e user filled out every correctly data and not go past the limit .
Negative Testcase :-Not to add future date , Not to specify negative amount.
Restricted User :-Test whether a user with restricted access used in your code .
10. Test class should be annotated with @isTest .
11 . @isTest annotation with test method  is equivalent to testMethod keyword .
12. Test method should static and no void return type .
13. Test class and method default access is private ,no matter to add access specifier .
14. classes with @isTest annotation can't be a interface or enum .
15. Test method code can't be invoked by non test request .
16. Stating with salesforce API 28.0 test method can not reside inside non test classes .
17. @Testvisible annotation to make visible private methods inside test classes.
18. Test method can not be used to test web-service call out . Please use call out mock .
19. You can't  send email from test method.
20.User, profile, organization, AsyncApexjob, Corntrigger, RecordType, ApexClass, ApexComponent ,ApexPage we can access without (seeAllData=true) .
21. SeeAllData=true will not work for API 23 version eailer .
22. Accessing static resource test records in test class e,g List<Account> accList=Test.loadData(Account,SobjectType,'ResourceName').
23. Create TestFactory class with @isTest annotation to exclude from organization code size limit .
24. @testSetup to create test records once in a method  and use in every test method in the test class .
25. We can run unit test by using Salesforce Standard UI,Force.com IDE ,Console ,API.
26. Maximum number of test classes run per 24 hour of period is  not grater of 500 or 10 multiplication of test classes of your organization.
27. As apex runs in system mode so the permission and record sharing are not taken into account . So we need to use system.runAs to enforce record sharing .
28. System.runAs will not enforce user permission or field level permission .
29. Every test to runAs count against the total number of DML issued in the process .


Please let us know if this post will help you
 
chandu kumarchandu kumar
Hi amith,
Can you please write test code for one of  those pagereference methods?