2

Can any one help me out with test class code coverage .I have written a test class for a controller having wrapper class,but the code covearage is zero. Any help very much appreciated.

Different types of Error while running the test :

1)When the query on standard is removed.

i.e.PriceBook2 pb2Standard = [select Id from Pricebook2 where IsStandard=true]; when i comment this line ,i get an error as

System.DmlException: Insert failed. First exception on row 0; first error: STANDARD_PRICE_NOT_DEFINED, No standard price defined for this product: []

2)when i dont comment this line ,i get an error as

i.e.PriceBook2 pb2Standard = [select Id from Pricebook2 where IsStandard=true];`

System.QueryException: List has no rows for assignment to SObject

Line is :Pricebook2 pb2 = new Pricebook2(Name='NONDIE');

3)When i add (seealldata=true) for @istest annotation , the test class get executes ,if i dont give (seealldata=true) for @istest annotation , the test class does not get executed.

Test Code **

@istest
Public class  TestInvoiceController{
static Testmethod void Invoicemethodtest(){

 test.startTest();

 Date closeDt = Date.Today();

// set up Account and Verify that the results are as expected.
Account a = new Account();
a.Name = 'icrm test acc';
insert a;
// set up opportunity and Verify that the results are as expected.
opportunity op = new opportunity(Name='test DIE 4/6/2015' ,  AccountId= a.Id,StageName = 'Prospecting', 
                                   CloseDate = closeDt, Shipment_Info__c='test',
                                   Shipping_Address_Line1__c='Road No 37',
                                   Shipping_Address_Line2__c='Jubilee Hills',
                                   Shipping_Address_Line3__c='Land Mark Neerus',
                                   Shipping_City__c='Hyd',
                                   Shipping_State_Province__c='TS',
                                   Shipping_Zipcode__c='500081',
                                   Shipping_Country__c ='India',
                                   Bill_of_Lading__c='This is waybill1 this is waybill 2 waybill3 -34958309458 waybill - 44570375');

  insert op;


  // set up product and Verify that the results are as expected.

  Product2 pro = new Product2(Product_Line__c='DIE',Name='BXCD',Product_Code_Item_Number__c='BXCD24',isActive=true);
  insert pro;


   // set up pricebook and Verify that the results are as expected.

 Pricebook2 pb2 = new Pricebook2(Name='DIE');
 insert pb2;
  PriceBook2 pb2Standard = [select Id from Pricebook2 where IsStandard=true];


   // set up pricebookentry and Verify that the results are as expected.

PricebookEntry pbe =new PricebookEntry(unitprice=0.01,Product2Id=pro.Id,Pricebook2Id=pb2Standard.Id,isActive=true,UseStandardPrice = false
);
 insert pbe;


  // set up opportunitylineitem and Verify that the results are as expected.

 OpportunityLineItem OPplineitem= new OpportunityLineItem (Quantity=2, OpportunityId=op.Id,UnitPrice=0.01,PriceBookEntryId=pbe.Id);
 insert OPplineitem;

  test.stopTest();

  InvoiceController ic = new InvoiceController();
  apexpages.currentpage().getparameters().put('opId',op.id);


  }

 static Testmethod void Invoicemethodtest1(){
 test.startTest();

 Date closeDt = Date.Today();


  // set up account and Verify that the results are as expected.

 Account a = new Account();
a.Name = 'icrm test acc';
insert a;
// set up opportunity and Verify that the results are as expected.
opportunity op = new opportunity(Name='test NonDIE 4/6/2015' , AccountId= a.Id,StageName = 'Prospecting', 
                                   CloseDate = closeDt,Shipment_Info__c='test',
                                   Shipping_Address_Line1__c='Road No 37',
                                   Shipping_Address_Line2__c='Jubilee Hills',
                                   Shipping_Address_Line3__c='Land Mark Neerus',
                                   Shipping_City__c='Hyd',
                                   Shipping_State_Province__c='TS',
                                   Shipping_Zipcode__c='500081',
                                   Shipping_Country__c ='India' , 
                                   Bill_of_Lading__c='This is waybill1 this is waybill 2 waybill3 -34958309458 waybill - 44570375');

  insert op;

  // set up product and Verify that the results are as expected.
  Product2 pro = new Product2(Product_Line__c='NONDIE',Name='BXRC',Product_Code_Item_Number__c='BXRC27',isActive=true);
  insert pro;


  // set up pricebook and Verify that the results are as expected.
  Pricebook2 pb2 = new Pricebook2(Name='NONDIE');
  insert pb2; 
PriceBook2 pb2Standard = [select Id from Pricebook2 where IsStandard=true];


  // set up pricebookentry and Verify that the results are as expected.


  PricebookEntry pbe = new PricebookEntry(unitprice=1,Product2Id=pro.Id,isActive=true,Pricebook2Id=pb2Standard.Id,UseStandardPrice = false
);
   insert pbe;


 // set up opportunitylineitem and Verify that the results are as expected.

 OpportunityLineItem OPplineitem= new OpportunityLineItem (Quantity=2, OpportunityId=op.Id,UnitPrice=1,PriceBookEntryId=pbe.id );

  insert OPplineitem;

  test.stopTest();

  InvoiceController ic = new InvoiceController();

  apexpages.currentpage().getparameters().put('opId',op.id);

     }                        
  }

Controller :

public with sharing class InvoiceController {
           List<wrapperClass> appointmentList {get;set;}
           List<wrapperClass> appointmentList1 {get;set;} 
           public list<string> address{get;set;}
           public string address1{get;set;}
           public string address2{get;set;}
           public string address3{get;set;}
           public string address4{get;set;}
           public string address5{get;set;}
           public string address6{get;set;}
           public string address7{get;set;}
           public String showaddressonVf{get;set;}
           public string wholeaddress{get;set;}


   //Added by sumit
               List<String> stringList ;
               public String all{get;set;}
           //------------
   public InvoiceController(){
   list<opportunity> op=[select id,Shipment_Info__c
   from opportunity where id=:apexpages.currentpage().getparameters().get('id')];
    //opportunity op=[select id,Shipment_Info__c from opportunity where id=:apexpages.currentpage().getparameters().get('id')];
       //---------------------Added by sumit----------------------
       stringList= new List<String>(); 
       if(op.size()>0)
       if(op[0].Shipment_Info__c!=NULL && op[0].Shipment_Info__c!=''){
           List<String> stringList=(op[0].Shipment_Info__c).split(',');
               for(integer i=0;i<stringList.size();i++){
                   all=all+stringList[i]+'<br/>';
               }

           all=all.remove('null');      
        }
      //------------------------------------------------------------
   }
       public Id OppId{get;set;}



       public InvoiceController(ApexPages.StandardController sc) {
       oppId=Apexpages.currentPage().getparameters().get('Id');
   }


          public List<wrapperClass> getappointmentList (){
                              wrapperClass tempObj;
               tempObj = new wrapperClass();

               List<wrapperClass> aReList = new List<wrapperClass>(); 
           List<Opportunity> opp=[select id,Name,AccountId from Opportunity where id=:oppid limit 1];
           List<opportunity> Opp1=[select id,name,CreatedDate from opportunity where id=:oppid];
           //Opportunity opp1=[select id,Name,AccountId from Opportunity where id=:oppid limit 1];
           //Opportunity Opp=[select id,name,CreatedDate from opportunity where id=:oppid ];
                          List<OpportunityLineItem> OPplineitem= [SELECT OpportunityId,Quantity,PricebookEntry.Product2.Name,Sys_total_Amount__c,LN__c,Extension__c,ListPrice
                          ,Pick__c,Schedule_Ship_date__c,Bin_Item_Code__c ,PricebookEntry.Product2.Product_Line__c,OpportunityLineItem.Opportunity.Subtotal__c,OpportunityLineItem.Opportunity.Non_Pick_Total__c,
                           OpportunityLineItem.Opportunity.Pick_Total__c,Newextension__c,unit_price__c ,ItemNumber__c,product_code__c FROM OpportunityLineItem where OpportunityId=:oppId]; 
                          system.debug('HHHHHHHHHH'+OPplineitem.size());

                       for(integer i=0; i < OPplineitem.size(); i++)
                       {
                     tempObj = new wrapperClass();
                     tempObj.productname= OPplineitem[i].PricebookEntry.Product2.Name;
                     tempObj.BinItemCode=OPplineitem[i].Bin_Item_Code__c;
                     tempObj.quantity=OPplineitem[i].Quantity;
                     tempObj.unitprice=OPplineitem[i].ListPrice;
                     tempobj.extension=OPplineitem[i].Extension__c ;
                     tempObj.totalamount=OPplineitem[i].Sys_total_Amount__c;
                     tempObj.Subtotal =OPplineitem[i].Opportunity.Subtotal__c;
                     tempObj.NonPickTotal=OPplineitem[i].Opportunity.Non_Pick_Total__c;
                     tempObj.PickTotal=OPplineitem[i].Opportunity.Pick_Total__c;
                     tempObj.Newextension=OPplineitem[i].Newextension__c;
                     tempObj.Unitprice=OPplineitem[i].unit_price__c;
                     tempObj.ItemNumber=OPplineitem[i].ItemNumber__c;
                     tempObj.productcode=OPplineitem[i].product_code__c;
                     tempobj.LineNumber=OPplineitem[i].LN__c;
                     tempobj.Pick= OPplineitem[i].Pick__c;
                     tempobj.ScheduledShipDate = Opplineitem[i].schedule_ship_date__c;

                     if(tempobj!=null){
                        aReList.add(tempObj);
                     }
                    }

           return aReList;
 }

 public List<wrapperClass> getappointmentList1 (){
           wrapperClass tempObj1 ; 
           List<wrapperClass> aReList1 = new List<wrapperClass>();
                   List<Opportunity> opp=[select id,Name,AccountId,Shipment_Info__c,
                    Shipping_Address_Line1__c,
                        Shipping_Address_Line2__c,
                        Shipping_Address_Line3__c,
                        Shipping_City__c,
                        Shipping_State_Province__c,
                        Shipping_Zipcode__c,
                        Shipping_Country__c  
                        from Opportunity where id=:oppid];
           for(integer i=0; i < opp.size(); i++)
           {
             tempObj1 = new wrapperClass();
             tempObj1.billingAddress= opp[i].Shipment_Info__c;
                               }
             aReList1.add(tempObj1);        

             /* Below code commented by Kiranmai as Shipment Info field is split into multiple fields

             //------- Added by Sumit-----------  
             stringList= new List<String>(); 

             if(aReList1[0].billingAddress!=NULL && aReList1[0].billingAddress!=''){
                List<String> stringList=(aReList1[0].billingAddress).split(',');
                for(integer i=0;i<stringList.size();i++){
                   all=all+stringList[i]+'<br/>';
                }
             } */

             all = all + opp[0].Shipping_Address_Line1__c + '<br/>';
             if(opp[0].Shipping_Address_Line2__c!=NULL && opp[0].Shipping_Address_Line2__c!=''){
                 all = all + opp[0].Shipping_Address_Line2__c + '<br/>';
             }

             if(opp[0].Shipping_Address_Line3__c!=NULL && opp[0].Shipping_Address_Line3__c!=''){
                 all = all + opp[0].Shipping_Address_Line3__c + '<br/>';
             } 

             if(opp[0].Shipping_City__c!=NULL && opp[0].Shipping_City__c!=''){
                 all = all + opp[0].Shipping_City__c + '<br/>';
             }

             if(opp[0].Shipping_State_Province__c!=NULL && opp[0].Shipping_State_Province__c!=''){
                 all = all + opp[0].Shipping_State_Province__c + '<br/>';
             }  

             if(opp[0].Shipping_Zipcode__c!=NULL && opp[0].Shipping_Zipcode__c!=''){
                 all = all + opp[0].Shipping_Zipcode__c + '<br/>';
             }

             if(opp[0].Shipping_Country__c!=NULL && opp[0].Shipping_Country__c!=''){
                 all = all + opp[0].Shipping_Country__c + '<br/>';
             }
            /* all = all + opp[0].Shipping_City__c + ', '+opp[0].Shipping_State_Province__c + ' - ' + opp[0].Shipping_Zipcode__c + '<br/>';
             all = all + opp[0].Shipping_Country__c + '<br/>';*/

             all=all.remove('null');

                /*-----------------------------------*/ 
      return aReList1;  
   }
           public class wrapperClass{
          // public String Item{get;private set;}
           public String productname{get; set;}
           public String BinItemCode{get;set;}
           public Decimal Quantity{get;set;}
           public Decimal totalamount{get;set;}
           public Decimal Subtotal{get;set;}
           public Decimal NonPickTotal{get;set;} 
           public Decimal PickTotal{get;set;}                             
           public Decimal unitprice{get;set;}
           public Decimal Newextension{get;set;}
           public String ItemNumber{get;set;}
           public date todaysdate{get;set;}
           public decimal extension{get;set;}
           public string LineNumber{get;set;}
           public string productcode{get;set;}
           public string billingAddress{get;set;}
           public string splittedaddress{get;set;}
           public string productline{get;set;}
           public string wholeaddress{get;set;}
           public boolean Pick{get;set;}
           public date ScheduledShipDate{get;set;}
           public wrapperClass(){}                
       }
 }
3

You can use Test.getStandardPricebookId() method to get the standard pricebook Id in test class. In this way, you don't need to perform any soql or write seealldata=true.

Please let me know if this makes sense.

  • :I tried in this way :Id stdId=Test.isRunningTest()? Test.getStandardPricebookId() : [SELECT Id From Pricebook2 WHERE IsStandard = true].Id; PricebookEntry pbe1=[SELECT ID FROM PricebookEntry WHERE Pricebook2Id = :stdId]; but it did not work.Any help very much appreciated. – nikkey Apr 14 '15 at 10:19
  • Please use it in your test class where you have used PriceBook2 pb2Standard = [select Id from Pricebook2 where IsStandard=true] soql. – Pankaj Ganwani Apr 14 '15 at 10:27
  • @ Pankaj Ganwani:I have commented the line PriceBook2 pb2Standard = [select Id from Pricebook2 where IsStandard=true] soql. and used the line Id stdId=Test.isRunningTest()? Test.getStandardPricebookId() : [SELECT Id From Pricebook2 WHERE IsStandard = true].Id; but it throws an Error as :System.DmlException: Insert failed. First exception on row 0; first error: STANDARD_PRICE_NOT_DEFINED, No standard price defined for this product: [] at line insert pbe; can you pricesely tell me whether the insert for Pricebook2 should be done.I shall appreciated your help – nikkey Apr 14 '15 at 10:38
  • I think you are using it in test class, not in controller class. If so, then you don't need to use Test.isRunningTest(). Simply use Test.getStandardPricebookId() in test class instead of soql. Check for the api versions as well. If they are not latest then set them to latest one(33.0) – Pankaj Ganwani Apr 14 '15 at 10:46
  • @ Pankaj Ganwani:Im using in test class ,the api version is (33.0) when I use Test.getStandardPricebookId() ,it throws the error again as STANDARD_PRICE_NOT_DEFINED,Correct me if I wrong ,I'm using as Pricebook2 pb2 = new Pricebook2(Name='DIE'); insert pb2;Test.getStandardPricebookId();PricebookEntry pbe =new PricebookEntry(unitprice=0.01,Product2Id=pro.Id,Pricebook2Id=pb2.Id,isActive=true,UseStandardPrice = false ); insert pbe; and it throws an Error at line insert pbe; – nikkey Apr 14 '15 at 11:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.