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
David OvellaDavid Ovella 

How to test this class?

Hello, I want to know how to Test this class? because I'm getting the error of coverage code
 
public class OTPresupuestoControllers {
   
    public List<cOT> cOTList{get;set;} 
    public List<cLdd> cLddList {get; set;}
    public List<cFac> cFacList {get; set;}
    public String otId;
    public String prefacturaId {get; set;}
    
    
    public OTPresupuestoControllers() {    
 
    }

    public String getPreFacturaId(){ 
        return prefacturaId;
    }
    
    public void setPreFacturaId(string PreFacturaId) {    
        prefacturaId = PreFacturaId;  
    }    

    
    public String getOtId(){ 
        return otId;
    }
    
    public void setOtId(string OTId) {    
        otId = OTId;  
        this.cOTList = new List<cOT>();
        for(OT__c o : [SELECT Id, Name, Total_1__c,Total_2__c,Total_3__c,Total_4__c,Total_5__c,
                       Nombre_del_Cliente_ppto__c, Total_a_Pagar__c, Archivo__c, 
                       Forma_de_Pago__c, Requiere_Sena__c,Nombre_de_la_cuenta__c,
                       Pre_Factura__r.Cuenta__r.Ex_Fiscal__c,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,Texto_largo_1_presup__c,
                       RUC__c,Pre_Factura__r.Tarjeteria__c,Sucursal__c,Firma__c
                       OT__c.Saldo_a_Pagar__c
                       From OT__c Where Id=:otId Limit 1])	
        			   {cOTList.add(new cOT(o));}  
        
        
        this.cLddList = new List<cLdd>();
        for(Linea_de_detalle__c x: [Select Id, Name, Codigo__c, Descripcion_del_Producto__c, Q__c,
                                    P_Unit_Linea__c, Total_Linea__c, Grupo_DP__c, createddate
                                    From Linea_de_detalle__c Where OT__c=:otId ORDER BY createddate])
        							{cLddList.add(new cLdd(x));} 
        
                
        
        this.cFacList = new List<cFac>();
        for(Factura__c z: [Select Id, Name, Saldo_Final__c      
                           From Factura__c Where Pre_Factura__c =:prefacturaId Limit 1])
        				   {cFacList.add(new cFac(z));} 
    }

  
    public class cOT{
        public OT__c OT{get; set;}
        public String totalapagar{get;set;}

        public cOT(OT__c o){
            this.OT = o;
            this.totalapagar= NumberToSpanishWords.convertToGs(o.Total_a_Pagar__c);
            }      
    }
          
    public class cLdd{           
        public Linea_de_detalle__c LDD {get; set;}           
        public string P_Unit_Linea {get; set;}
        public string Total_Linea {get; set;}          
        public string Q {get; set;}
                            
        public cLdd(Linea_de_detalle__c x){                
            this.LDD =  x;                
            this.P_Unit_Linea=NumberToSpanishWords.convertToGs(x.P_Unit_Linea__c);                
            this.Total_Linea=NumberToSpanishWords.convertToGs(x.Total_Linea__c);               
            this.Q=NumberToSpanishWords.convertToEU(x.Q__c);                 
        }
    }
    
  
    public class cFac{           
        public Factura__c FAC {get; set;}           
        public string saldofinal {get; set;}
                            
        public cFac(Factura__c z){                
            this.FAC =  z;                
            this.saldofinal=NumberToSpanishWords.convertToGs(z.Saldo_Final__c);                             
        }
    }     
}



 
Best Answer chosen by David Ovella
pconpcon
So you do not have a constructor that takes in an OT__c object.  Your test should look like
 
@isTest
public class OTPresupestoControllerTest  {
    public static testMethod void testOTPresupestoController() {  
        Pre_Factura__c prefactura = new Pre_Factura__c ();
        insert prefactura;

        OT__c ot = new OT__c(
            Pre_Factura__c = prefactura.id,
            Utilizar_DP__c=true,
            Total_1__c= 100000,
            Total_2__c= 100000
            /*more fields*/
        );

        insert ot;

        OTPresupuestoControllers otts = new OTPresupuestoControllers();

        Test.startTest();

        otts.setOtId(ot.Id);
        List<OTPresupuestoControllers.cOT> results = otts.cOTList;

        Test.stopTest();

        System.assertEquals(
            1,
            results.size(),
            'Did not get the expected number of results back'
        );
        System.assertEquals(
            prefactura.Id,
            results.get(0).ot.Pre_Factura__c,
            'Did not get the right value back for Pre_Factura__c'
        );
        System.assertEquals(
            ot.Total_1__c,
            results.get(0).ot.Total_1__c,
            'Did not get the right Total 1 back'
        );
        System.assertEquals(
            'ExpectedPagar',
            results.get(0).totalapagar,
            'Did not get the expected pagar'
        );
    }
}
NOTE: This code has not been tested and may contain typographical or logical errors

Take a look over this [1] to learn more about testing strategies

[1] http://blog.deadlypenguin.com/blog/testing/strategies/

All Answers

pconpcon
I would recommend that you do some reading on testing [1] [2] [3] [4] to get a better understanding.  Each of your individual tests should only tests one specific portion of you class (ie a constructor test, sendEmail test, contactSelected test, etc).  You should also have both a postitive (everything works perfectly) and a negative (things are not right) test.

Each test should follow the following structure:
  • Setup of test data. This includes creation of any data needed by your class.  Account, Contacts etc
  • Starting the test. This is calling Test.startTest() to reset the governor limits
  • Calling your class / method
  • Stopping the test.This is calling Test.stopTest() to reset the governor limits and allow for any async jobs to finish
  • Asserting that your changes have worked
    • If you have inserted/updated/deleted data, you need to query for the updates
    • Run System.assert, System.assertEquals, System.assertNotEquals to verify that you got the correct data back
If you have any specific problems with your tests, feel free to create a new post with the part of the class you are trying to test and your current test method, and you will more likely get a better response then asking for someone to essentially write an entire test class for you.

[1] http://www.sfdc99.com/2013/05/14/how-to-write-a-test-class/
[2] http://pcon.github.io/presentations/testing/
[3] http://blog.deadlypenguin.com/blog/2014/07/23/intro-to-apex-auto-converting-leads-in-a-trigger/
[4] http://blog.deadlypenguin.com/blog/testing/strategies/
David OvellaDavid Ovella
thanks for answer.
I don't know how to test because I got an error of constructor.

I just need a little example of how to test a constructor or a list

part of my apex, if I get I liitle a example I can then see how to do the others one.
public class OTPresupuestoControllers {
   
    public List<cOT> cOTList{get;set;} //my list
    public String otId;

    //constructor
    public OTPresupuestoControllers(){    
    }

    //I get the Id from a vf component
    public String getOtId(){ 
        return otId;
    }
    
    //don't know how to test this kind of list
    public void setOtId(string OTId){    
        otId = OTId;  
        this.cOTList = new List<cOT>();
        for(OT__c o : [SELECT Id, Name, Total_1__c,Total_2__c,Total_3__c,
                               Total_4__c,Total_5__c,
                               From OT__c Where Id=:otId Limit 1])	
        		               {cOTList.add(new cOT(o));}     
    }
  
    public class cOT{
        public OT__c OT{get; set;}
        public String totalapagar{get;set;}

        public cOT(OT__c o){
            this.OT = o;
            this.totalapagar= NumberToSpanishWords.convertToGs(o.Total_a_Pagar__c);
            }      
    }
}


my lillte test
@isTest
public class OTPresupestoControllerTest {
       
    public static testMethod void testOTPresupestoController() {  
      	 Pre_Factura__c prefactura = new Pre_Factura__c ();
         insert prefactura;
       
         OT__c ot = new OT__c(Pre_Factura__c=prefactura.id, Utilizar_DP__c=true);
          /*Write code to populate fields. See my note below*/       
      
            ot.Cant_1__c= 100000;
            ot.Cant_2__c= 100000;
            /*more fields*/

            insert ot;
     
/*and in this part I don't know how to connect this test with my Constructor because I don't have a apex controller (ApexPages.StandardController controller)
THE ERROR: Constructor not defined: [OTPresupuestoControllers].&lt;Constructor&gt;(ApexPages.StandardController)
*/

         /*ApexPages.StandardController ots = new ApexPages.StandardController(ot);      
         OTPresupuestoControllers otts = new OTPresupuestoControllers(ots);*/
      
    }

}






 
pconpcon
So you do not have a constructor that takes in an OT__c object.  Your test should look like
 
@isTest
public class OTPresupestoControllerTest  {
    public static testMethod void testOTPresupestoController() {  
        Pre_Factura__c prefactura = new Pre_Factura__c ();
        insert prefactura;

        OT__c ot = new OT__c(
            Pre_Factura__c = prefactura.id,
            Utilizar_DP__c=true,
            Total_1__c= 100000,
            Total_2__c= 100000
            /*more fields*/
        );

        insert ot;

        OTPresupuestoControllers otts = new OTPresupuestoControllers();

        Test.startTest();

        otts.setOtId(ot.Id);
        List<OTPresupuestoControllers.cOT> results = otts.cOTList;

        Test.stopTest();

        System.assertEquals(
            1,
            results.size(),
            'Did not get the expected number of results back'
        );
        System.assertEquals(
            prefactura.Id,
            results.get(0).ot.Pre_Factura__c,
            'Did not get the right value back for Pre_Factura__c'
        );
        System.assertEquals(
            ot.Total_1__c,
            results.get(0).ot.Total_1__c,
            'Did not get the right Total 1 back'
        );
        System.assertEquals(
            'ExpectedPagar',
            results.get(0).totalapagar,
            'Did not get the expected pagar'
        );
    }
}
NOTE: This code has not been tested and may contain typographical or logical errors

Take a look over this [1] to learn more about testing strategies

[1] http://blog.deadlypenguin.com/blog/testing/strategies/
This was selected as the best answer
David OvellaDavid Ovella
it works perfect, I was having 73% of code coverage so with this test it let me passed to production
thanks pcon
pconpcon
I would highly recommend that you increase your test coverage above what I gave you (you should be able to easily reach 100% for this class) and also test for things outside the normal positive use case.