0

i have following Controller of select list and select option.i just make the test class of that controller but i am not covered the whole part of the controller.here is my controller,test class and snapshot of testclass that covered...


Public Class ObjectRelation {  
    Public List<Product2> pro{get;set;}
    public List<SelectOption> AccountOptions {get;set;}
    public List<SelectOption> OpportunityOptions{get;set;}
    public List<SelectOption> ProductOptions{get;set;}
    Public String AccName{get;set;}
    Public String OppName{get;set;}
    Public String prodName{get;set;}
    Public ObjectRelation()
    {   
        AccountOptions = new List<SelectOption>();
        AccountOptions.add(new SelectOption('none','----Select Account----'));
        for (Account record : [select Id, Name from Account])
        {
          AccountOptions.add(new SelectOption(record.Id,record.Name));
        }                     
    } 
     Public List<SelectOption> getOpp(){
               OpportunityOptions = new List<SelectOption>();
               OpportunityOptions.add(new SelectOption('none','----Select Opportunity----'));
              for (Opportunity record : [select id,name from Opportunity where Account.Id =: AccName] )
                {
                      OpportunityOptions.add(new SelectOption(record.Id,record.Name));
                }  
                System.Debug('####'+OpportunityOptions);
                return OpportunityOptions;
     }
     Public List<SelectOption> getProd(){
        ProductOptions = new List<SelectOption>();
        ProductOptions.add(new SelectOption('none','----Select Product----'));
        for (OpportunityLineItem record : [SELECT Id,PricebookEntry.Product2Id,PricebookEntry.Product2.Name FROM OpportunityLineItem WHERE OpportunityId =: OppName] )
        {
              ProductOptions.add(new SelectOption(record.PricebookEntry.Product2Id,record.PricebookEntry.Product2.Name));
        }  
        return ProductOptions;
     }
     Public List<Product2> GetDisplayProduct(){
          pro = new List<Product2>();
          pro = [Select id,Name,ProductCode,Description,IsActive from Product2 where id =: prodName ];
          if(OppName == '0069000000DY2nV'){
          System.debug('@'+prodName);
        }
        else{
            System.debug('@@'+prodName);
        }
          string sString = 'some*Stuff*vivek';
        return pro;       
     }
     public PageReference Save(){         
         try{
                if(pro.size() > 0){
                  upsert pro;    
                  ApexPages.addMessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Record Successfully Updated'));   
                }
            } catch (DMLException e){
                // show the messages to the user
                ApexPages.addMessages(e);
            }             
         return null;
     }
}

Test Class of the Above Controller

@isTest(SeeAllData=true)
Public class TestObjectRelation {
    Static TestMethod void TestObjectRelation (){  
        PageReference pageRef = Page.WrapperStandard;
        Test.setCurrentPage(pageRef);       
        ObjectRelation objMain = new ObjectRelation();   
        Account acc = new Account(name='vivek113');
        insert acc;
        Opportunity opp = new Opportunity(AccountId=acc.id,name='opp',StageName='abc',CloseDate=System.today(),optname__c='opp');
        insert opp;

        /* List<SelectOption> OpportunityOptions = new List<SelectOption>();
         Opportunity record = [select id,name from Opportunity where Account.Id =: AccName];

         OpportunityOptions.add(new SelectOption(record.Id,record.Name));*/
        Product2 prod = new Product2(name='new',IsActive = true);
        insert prod;            
        Pricebook2 objprice = [select Id from PriceBook2 where IsStandard=True];
        System.debug('****'+objprice);
        objprice.IsActive = true;
        update objprice;            
        PricebookEntry standardPrice= new PricebookEntry (Product2Id = prod.id,unitprice=20,Pricebook2Id = objprice.id,UseStandardPrice=false,isActive=true);
        insert standardPrice;       
        OpportunityLineItem objoppline = new OpportunityLineItem (OpportunityId=opp.id,pricebookentryId=standardprice.id,Quantity=10,totalprice=40);
        insert objoppline;           
        objMain.getopp();
        objMain.getprod(); 
        objMain.getdisplayProduct();
        objMain.save();
    }
}

Snapshot to cover...

enter image description here

Here now help me to cover the red part of the test class....Thanks for advance...Please....

2 Answers 2

1

About this query

[select id,name from Opportunity where **Account.Id =: AccName**]

Id = name? You should think about variables names. I In my opinion this code need:

  • System.Assertions in test.

  • You need to set those variables from controller AccName, OppName

2
  • Sorry sir but AccName(Actually it is id) is a variable that is used in VFPage in selectlist value part.....
    – Vivek113
    Commented Feb 12, 2014 at 6:18
  • Thats why I put "think about variables names" and didn't say that those are wrong types ;) But what more important - did setting those values solved your problems? Commented Feb 12, 2014 at 6:21
1

Your queries are not returning a value, because you didn't "select" a value.

Set objMain.AccName to a valid ID value before calling objMain.getOpp(), such as objMain.AccName = acc.Id;.

You'll also need to satisfy the condition for the other query as well.

3
  • Thanks u sfdcfox...it's really working....thank u so much....
    – Vivek113
    Commented Feb 12, 2014 at 6:26
  • now i am testing for the 200 records....so how can i put objmain.AccName = acc.id in the for loop
    – Vivek113
    Commented Feb 12, 2014 at 7:36
  • Here is my For loop...for(Integer i=0;i<200;i++){ Account acc = new Account(name='vivek113'+i); lstacc.add(acc); objMain.AccName = lstacc[i].Id; } insert lstacc;
    – Vivek113
    Commented Feb 12, 2014 at 7:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .