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
nikkeynikkey 

Code coverage in Test class appears only 4%

Can any one help me out with test class code coverage .I have written a test class for a controller,but the code covearage in the test class appears only 4% and the overall code coverage is 80%.

When i move from sandbox to production it throws an Error at production

Error :
Code Coverage Failure
Your organization's code coverage is 71%. You need at least 75% coverage to complete this deployment.

Details: Average test coverage across all Apex Classes and Triggers is 71%, at least 75% test coverage is required.

Here is the Code :
@istest(SeeAllData=true)

Public class  TestInvoiceController{

static Testmethod void Invoicemethodtest(){

 test.startTest();

 Date closeDt = Date.Today();

 //String standardPriceBookId = '';

// 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;
  Opportunity opp = [SELECT Name FROM Opportunity WHERE Id = :op.Id];
System.assertEquals('test DIE 4/6/2015', opp.Name);

  // 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;
  Product2 p2ex = [SELECT Name FROM Product2 WHERE Id = :pro.Id];
   System.assertEquals('BXCD', p2ex.Name);

   // 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];
//standardPriceBookId = pb2Standard.Id;

   // 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;
 PricebookEntry pbeex = [SELECT Pricebook2Id FROM PricebookEntry WHERE Id = :pbe.Id];
//System.assertEquals(pb2Standard, pbeex.Pricebook2Id);

  // 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();

 //String standardPriceBookId = '';

  // 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;
  Opportunity opp = [SELECT Name FROM Opportunity WHERE Id = :op.Id];
System.assertEquals('test NonDIE 4/6/2015', opp.Name);

  // 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;
  Product2 p2ex = [SELECT Name FROM Product2 WHERE Id = :pro.Id];
System.assertEquals('BXRC', p2ex.Name);


  // 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];
 // standardPriceBookId = pb2Standard.Id;
  // 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;
  PricebookEntry pbeex = [SELECT Pricebook2Id FROM PricebookEntry WHERE Id = :pbe.Id];
//System.assertEquals(pb2Standard, pbeex.Pricebook2Id); 

 // 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;
  OpportunityLineItem oliex = [SELECT PriceBookEntryId FROM OpportunityLineItem WHERE Id = :OPplineitem.Id];
System.assertEquals(pbe.Id, oliex.PriceBookEntryId); 

  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);        



             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/>';
             }

      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(){}                
       }
 }

Any help very much appreciated
PratikPratik (Salesforce Developers) 

Hi Nikkey,

 Here are some guidelines:

In the case of conditional logic (including ternary operators), execute each branch of code logic.

Make calls to methods using both valid and invalid inputs.

Complete successfully without throwing any exceptions, unless those errors are expected and caught in a try…catch block.

Always handle all exceptions that are caught, instead of merely catching the exceptions.

Use System.assert methods to prove that code behaves properly.

Use the runAs method to test your application in different user contexts.

Exercise bulk trigger functionality—use at least 20 records in your tests.

Use the ORDER BY keywords to ensure that the records are returned in the expected order.

Do not assume that record IDs are in sequential order.

Set up test data:

Create the necessary data in test classes, so the tests do not have to rely on data in a particular organization.

Create all test data before calling the Test.startTest method.

Since tests don't commit, you won't need to delete any data.

Write comments stating not only what is supposed to be tested, but the assumptions the tester made about the data, the expected outcome, and so on.

Test the classes in your application individually. Never test your entire application in a single test.

For more information:  https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_best_practices.htm

Thanks,
Pratik
nikkeynikkey
Hi Pratik , Thanks for your guidelines. Can u please help me out in the code for writing the test class.