+ Start a Discussion
David OvellaDavid Ovella 

How to test this apex class?

Hello, I'm trying to deploy an apex class to production but I can't because it gives me 72% of coverage code.
This is my apex class;
Does anyone know how to test this apex class and increase the coverage code? I really will appreciate any help

public with sharing class FacturaPDFController {

    public final Factura__c fct;
    public cFactura cfct{get; set;}
    public OT__c ots{get; set;}
    public xOT otts {get; set;}
    public Integer total{get; set;}
    public List<cOT> cOTList{get;set;}
    public List<cLdd> cLddList{get;set;}

    public FacturaPDFController(ApexPages.StandardController controller) {
        this.fct =  [Select Id, Pre_Factura__r.ST_Exentas__c, Pre_Factura__r.Descuento__c, Pre_Factura__r.ST_Gravadas_IVA10__c,                         Pre_Factura__r.Total_a_Pagar__c, Pre_Factura__r.IVA_10__c From Factura__c  Where Id = :controller.getId()];
        this.cfct = new cFactura(this.fct);
        total = Integer.valueOf( this.fct.Pre_Factura__r.Total_a_Pagar__c );



        //OT personalizado
        this.ots= [Select Cant_1__c,Cant_2__c,Cant_3__c,Cant_4__c,Cant_5__c, Texto_1_fact__c,Texto_2_fact__c,Texto_3_fact__c,
        Texto_4_fact__c,Texto_5_fact__c, Precio_Unit_1__c, Precio_Unit_2__c, Precio_Unit_3__c, Precio_Unit_4__c, Precio_Unit_5__c,
        Total_Ex_1__c, Total_Ex_2__c, Total_Ex_3__c, Total_Ex_4__c, Total_Ex_5__c,
        Total_Gra_1__c, Total_Gra_2__c, Total_Gra_3__c, Total_Gra_4__c, Total_Gra_5__c        
        from OT__c  where Pre_Factura__c = :this.fct.Pre_Factura__r.id];
        this.otts=new xOT(this.ots);
        
        
        
        //OT List
        this.cOTList = new List<cOT>();
        for(OT__c o : [Select Id, Codigo_2__c, Cantidad_2__c, Descripcion_2__c, Sub_Total__c, Exentas_2__c, Gravadas_IVA_10_2__c
        from OT__c  where Pre_Factura__c = :this.fct.Pre_Factura__r.id LIMIT 10]){
            cOTList.add(new cOT(o));
        }

        //LDD List
        this.cLddList = new List<cLdd>();
        for(Linea_de_detalle__c l : [Select Q__c, P_Unit_Linea__c, Total_Exentas__c, Total_Gravadas_IVA_10__c, Producto__r.Codigo__c,                   Producto__r.Descripcion__c from Linea_de_detalle__c Where OT__r.Pre_Factura__c = :this.fct.Pre_Factura__r.id LIMIT 10]){
            cLddList.add(new cLdd(l));
        }
    }



    public String getNumberToWords(){
        return NumberToSpanishWords.toSpanishWords( total );
    }


    public class cFactura{
        public Factura__c fct{get; set;}
        public String ST_Exentas{get;set;}
        public String ST_Gravadas_IVA10{get;set;}
        public String Total_a_Pagar{get;set;}
        public String IVA_10{get;set;}
        public String Descuento{get;set;}

        public cFactura(Factura__c f){
            this.fct = f;
            this.ST_Exentas = NumberToSpanishWords.convertToEU(f.Pre_Factura__r.ST_Exentas__c);
            this.ST_Gravadas_IVA10 = NumberToSpanishWords.convertToEU(f.Pre_Factura__r.ST_Gravadas_IVA10__c);
            this.Total_a_Pagar = NumberToSpanishWords.convertToEU(f.Pre_Factura__r.Total_a_Pagar__c);
            this.IVA_10 = NumberToSpanishWords.convertToEU(f.Pre_Factura__r.IVA_10__c);
            this.Descuento = NumberToSpanishWords.convertToEU(f.Pre_Factura__r.Descuento__c);
        }
    }
    
    
    public class xOT{ //valores que se usa para la OT personalizada
        public OT__c ots{get; set;}
        public string cantper1 {get; set;}
        public string cantper2 {get; set;}
        public string cantper3 {get; set;}
        public string cantper4 {get; set;}
        public string cantper5 {get; set;}
        public string precio1 {get; set;}
        public string precio2 {get; set;}
        public string precio3 {get; set;}
        public string precio4 {get; set;}
        public string precio5 {get; set;}
        public string totalexenta1 {get; set;}
        public string totalexenta2 {get; set;}
        public string totalexenta3 {get; set;}
        public string totalexenta4 {get; set;}
        public string totalexenta5 {get; set;}
        public string totalgravada1 {get; set;}
        public string totalgravada2 {get; set;}
        public string totalgravada3 {get; set;}
        public string totalgravada4 {get; set;}
        public string totalgravada5 {get; set;}
        
        
        public xOT(OT__c ot2) {
            this.ots=ot2;
            this.cantper1= NumberToSpanishWords.convertToEU(ot2.Cant_1__c);            
            this.cantper2= NumberToSpanishWords.convertToEU(ot2.Cant_2__c);
            this.cantper3= NumberToSpanishWords.convertToEU(ot2.Cant_3__c);
            this.cantper4= NumberToSpanishWords.convertToEU(ot2.Cant_4__c);
            this.cantper5= NumberToSpanishWords.convertToEU(ot2.Cant_5__c);
            this.precio1=NumberToSpanishWords.convertToEU(ot2.Precio_Unit_1__c);
            this.precio2=NumberToSpanishWords.convertToEU(ot2.Precio_Unit_2__c);
            this.precio3=NumberToSpanishWords.convertToEU(ot2.Precio_Unit_3__c);
            this.precio4=NumberToSpanishWords.convertToEU(ot2.Precio_Unit_4__c);
            this.precio5=NumberToSpanishWords.convertToEU(ot2.Precio_Unit_5__c);
            this.totalexenta1=NumberToSpanishWords.convertToEU(ot2.Total_Ex_1__c);
            this.totalexenta2=NumberToSpanishWords.convertToEU(ot2.Total_Ex_2__c);
            this.totalexenta3=NumberToSpanishWords.convertToEU(ot2.Total_Ex_3__c);
            this.totalexenta4=NumberToSpanishWords.convertToEU(ot2.Total_Ex_4__c);
            this.totalexenta5=NumberToSpanishWords.convertToEU(ot2.Total_Ex_5__c);
            this.totalgravada1=NumberToSpanishWords.convertToEU(ot2.Total_Gra_1__c);
            this.totalgravada2=NumberToSpanishWords.convertToEU(ot2.Total_Gra_2__c);
            this.totalgravada3=NumberToSpanishWords.convertToEU(ot2.Total_Gra_3__c);
            this.totalgravada4=NumberToSpanishWords.convertToEU(ot2.Total_Gra_4__c);
            this.totalgravada5=NumberToSpanishWords.convertToEU(ot2.Total_Gra_5__c);
     }
     
    }       
            
    public class cOT{ //valores que se usa para cuando la Pre_Factura tiene mas de una OT
        public OT__c OT{get; set;}
        public String Sub_Total{get;set;}
        public String Exentas_2{get;set;}
        public String Gravadas_IVA_10_2{get;set;}

        public cOT(OT__c o){
            this.OT = o;
            this.Sub_Total = NumberToSpanishWords.convertToEU(o.Sub_Total__c);
            this.Exentas_2 = NumberToSpanishWords.convertToEU(o.Exentas_2__c);
            this.Gravadas_IVA_10_2 = NumberToSpanishWords.convertToEU(o.Gravadas_IVA_10_2__c);
            
        } 
    }

    public class cLdd{
        public Linea_de_detalle__c LDD{get; set;}
        public String P_Unit_Linea{get;set;}
        public String Total_Exentas{get;set;}
        public String Total_Gravadas_IVA_10{get;set;}

        public cLdd(Linea_de_detalle__c l){
            this.LDD = l;
            this.P_Unit_Linea = NumberToSpanishWords.convertToEU(l.P_Unit_Linea__c);
            this.Total_Exentas = NumberToSpanishWords.convertToEU(l.Total_Exentas__c);
            this.Total_Gravadas_IVA_10 = NumberToSpanishWords.convertToEU(l.Total_Gravadas_IVA_10__c);
        }
    }
}


this is the test that I diid but it gives me this error:
FacturaPDFControllerTest.testFacturaPDFController(), Details, System.QueryException: List has no rows for assigment to sObject Class.FacturaPDFController <ini>: line 21 column 1
Class.FacturaPDFControllerTest.testFacturaPDFController:line 22, column 1

@isTest
public class FacturaPDFControllerTest {
   public static testMethod void testFacturaPDFController() {
         Pre_Factura__c prefactura = new Pre_Factura__c ();
         insert prefactura;
       
         Factura__c fac = new Factura__c(Pre_Factura__c = prefactura.id);
          /*Write code to populate fields. See my note below*/ 
            fac.Pre_impreso__c=1234999;
            fac.Pre_impreso_001_001__c=1234999;
            fac.Pre_impreso_002_001__c=1234999;
            fac.Pre_impreso_002_002__c=1234999;
            fac.Pre_impreso_003_001__c=1234999;
            fac.Caja__c='Caja 1';
            fac.Serie__c='Serie 1';
            fac.Sucursal__c='Maker Villa Morra';
                
            insert fac;
 
   
         ApexPages.StandardController fact = new ApexPages.StandardController(fac);       
         FacturaPDFController facts = new FacturaPDFController(fact);
         facts.total=1000;
         facts.getNumberToWords();
         System.assert(facts.fct  <> NULL);

      }
  }
Vinnie BVinnie B
You should use the Developer Console to see which lines of your code are covered and which are not.  That will point you to the code that's not being covered better than any of us could.  :)

At first glance, I'd say you're going to need to create one of every object listed in your code as you did with the Factura__c object.