+ Start a Discussion
1111_forcecom1111_forcecom 

Test coverage apex classes

Hi developers,

In this opportunity my question comes from about the test classes and coverage from this to apex classes

I have two classes, one is Apex Class and the other is the Test Class. My question is why my Apex Class is 0% coverage??? Run the test class and this finish OK. Please guide me how can I for coverage the code in my Apex Class?

I put the code from the two classes.

Thanks for the support 

APEX CLASS 

public class contactExtension {

    private final Contact contact;
    private string idContact;
    private final Moneda__c monedabob;
    private string idMonedabob;
    private final Moneda__c monedausd;
    private string idMonedausd;
    public decimal x {get;set;}
    public decimal totalUsd {get;set;}

    public contactExtension(ApexPages.StandardController contactController) {
       this.contact = (Contact)contactController.getRecord();
       idContact = contact.id;
    }
         
    public List<Beneficio__c> getBeneficiosBob()
    {
        Moneda__c monedabob = [select Name from Moneda__c where Moneda__c.name = 'Bolivianos'];
        idMonedabob = monedabob.id;
        x = 0;
        for (Beneficio__c ben : [select monto_estimado__c from Beneficio__c where Contacto__c = :idContact and Moneda__c = :idMonedabob]){
            if (ben.monto_estimado__c == null){
                ben.monto_estimado__c = 0;
            }
            x = ben.monto_estimado__c + x;
        }
               
        return [select Catalogo_Beneficio__c, Name, Moneda__c, monto_estimado__c, proposito__c,fecha_entrega__c from Beneficio__c where Contacto__c = :idContact and Moneda__c = :idMonedabob];
    }
             
    public List<Beneficio__c> getBeneficiosUsd()
    {
        Moneda__c monedaUsd = [select Name from Moneda__c where Moneda__c.name = 'Dolares'];
        idMonedausd = monedausd.id;
       
        totalUsd = 0;
        for (Beneficio__c ben : [select monto_dolares__c from Beneficio__c where Contacto__c = :idContact and Moneda__c = :idMonedausd]){
            totalUsd = ben.monto_dolares__c + totalUsd;
        }       
        return [select Catalogo_Beneficio__c, Name, Moneda__c, monto_dolares__c, proposito__c,fecha_entrega__c  from Beneficio__c where Contacto__c = :idContact and Moneda__c = :idMonedausd];
    }
}

TEST CLASS

@isTest public class ContactTest {          
        testMethod private static void getBeneficiosBob()
        {
            Account a= new Account(Name = 'Fundacion Inti Raymi');
            insert a;
           
            Operacion__c operacion= new Operacion__c(Name = 'Kori Chaca');
            insert operacion;
           
            Departamento__c departamento = new Departamento__c(Name = 'Oruro');
            insert departamento;
           
            Provincia__c provincia= new Provincia__c(Name = 'Cercado', Departamento__c = departamento.id);
            insert provincia;
           
            Municipio__c municipio= new Municipio__c(Name = 'Caracollo', Provincia__c = provincia.id);
            insert municipio;
           
            Comunidad__c comunidad= new Comunidad__c(Name = 'Iroco');
            insert comunidad;           
           
            Organizacion__c organizacion= new Organizacion__c (Name = 'Iroco');
            insert organizacion;
                                               
            Moneda__c monedabob = new Moneda__c(Name = 'Bolivianos', sigla__c = 'BOB');
            insert monedabob;
           
            Contact contacto = new Contact(Firstname= 'Alejandra', Lastname = 'Helguero', Carnet_Identidad__c = '22334455', Operacion__c = operacion.id, Departamento__c = departamento.id, Provincia__c = provincia.id, Municipio__c = municipio.id, Comunidad__c = comunidad.id, Organizacion__c = organizacion.id);
            insert contacto;

            Categoria_beneficio__c categoria = new Categoria_beneficio__c(Name = 'Produccion Ganadera');
            insert categoria;
           
            Subcategoria_beneficio__c subcategoria = new Subcategoria_beneficio__c(Name = 'Ganaderos', Categoria_Beneficio__c = categoria.id);
            insert subcategoria;
           
            Catalogo_beneficios__c tipo_beneficio= new Catalogo_beneficios__c(Name= 'Sanidad Animal', Nombre__c = 'Sanidad Animal', Categoria_Producto__c = categoria.id, Subcategoria_producto__c = subcategoria.id);
            insert tipo_beneficio;           
           
            for(Integer i = 0; i < 20; i++){
                Beneficio__c b = new Beneficio__c(Contacto__c = contacto.id, Moneda__c = monedabob.id, monto_estimado__c = 100.56, Catalogo_Beneficio__c = tipo_beneficio.id, fecha_entrega__c = Date.newInstance(2014, 06 ,20));
                insert b;
            }           
           
            Moneda__c monedabobS = [select Name from Moneda__c where Moneda__c.name = 'Bolivianos'];
            String idMonedabob = monedabobS.id;
           
            Decimal x = 0;
            for (Beneficio__c ben : [select monto_estimado__c from Beneficio__c where Contacto__c = :contacto.id and Moneda__c = :monedabob.id]){
                if (ben.monto_estimado__c == null){
                    x = ben.monto_estimado__c + x;
                }
            }
            Beneficio__c[] beneficios = [select Catalogo_Beneficio__c, Name, Moneda__c, monto_estimado__c, proposito__c,fecha_entrega__c from Beneficio__c where Contacto__c = :contacto.id and Moneda__c = :monedabob.id];
            System.assert(beneficios != null, 'Retorna valores');
        }
}
Best Answer chosen by 1111_forcecom
lifewithryanlifewithryan
You're not referencing your extension class anywhere in the test.  You need to do something like:



//in your test method after setting up your data
ApexPages.StandardController ctl = new ApexPages.StandardController(contact);
ContactExtension ce = new ContactExtension(contact);

//and then excercise your methods here...(such as)
List<Beneficios__c> myBens = ce.GetBeneficios_Bob;
System.assertEquals(1, myBens.size()); //or whatever size you are expecting that list to be...

Hope that helps.  There was alot of code in your post and I admittedly skimmed it, but to me it looks like that is your issue.  You're not using your extension to test anything. (May have missed it)

All Answers

lifewithryanlifewithryan
You're not referencing your extension class anywhere in the test.  You need to do something like:



//in your test method after setting up your data
ApexPages.StandardController ctl = new ApexPages.StandardController(contact);
ContactExtension ce = new ContactExtension(contact);

//and then excercise your methods here...(such as)
List<Beneficios__c> myBens = ce.GetBeneficios_Bob;
System.assertEquals(1, myBens.size()); //or whatever size you are expecting that list to be...

Hope that helps.  There was alot of code in your post and I admittedly skimmed it, but to me it looks like that is your issue.  You're not using your extension to test anything. (May have missed it)
This was selected as the best answer
1111_forcecom1111_forcecom
Hi Ryan,

Thank you for your answer. Well I am not clear about the test connexions because I used the apex class for customize a piece of code for the view in my contact card. All the code above  is the apex class and the test of this class. I still don't understand why is necessary create the reference my extension, anyway I did that, following your example.

I modified the code of my test class, so I remain like this:

@isTest public class ContactTest {          
        testMethod private static void getBeneficiosBob()
        {   
           Contact contacto = new Contact(Firstname= 'Alejandra', Lastname = 'Helguero', Carnet_Identidad__c = '22334455', Operacion__c = operacion.id, Departamento__c = departamento.id, Provincia__c = provincia.id, Municipio__c = municipio.id, Comunidad__c = comunidad.id, Organizacion__c = organizacion.id);
            insert contacto;      
            ApexPages.StandardController ctl = new ApexPages.StandardController(contacto);
            contactExtension ce = new contactExtension(contacto);

            Beneficio__c[] beneficios = ce.getBeneficiosBob;
            System.assertEquals(1, beneficios.size());
        }
}

And now I have this error

Constructor not defined: [contactExtension].<Constructor>(SOBJECT:Contact)

¿Can you please explain me how it works the test of apex class?

Regards,

lifewithryanlifewithryan
change this: 
contactExtension ce = new contactExtension(contacto);

to:
contactExtension ce = new contactExtension(ctl);

You extension takes an instance of the StandardController, you seem to be passing in the contact. That was my fault though as I had a typo in my code up above. Sorry about that.
1111_forcecom1111_forcecom
Hi Ryan,

Now the error is the follow

Variable does not exist: getBeneficiosBob

In my class contactExtension getBeneficiosBob is a function, maybe is for that??

Thank you for your help


1111_forcecom1111_forcecom
Hi Ryan,

Tank you so much for your help. I resolve this issue and my test is now fine!!!

Tanks a lot.

Regards,