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
DeptonDepton 

need more test methods to reach the 75% (currently 67 %)

cannot deploy this trigger:

 

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

 

but before inserting this trigger when I go to Apex Classes and click on "run all tests" I got:

 

Code Coverage Total %85

 

 

 

the trigger I would like to deploy and I cannot is:

trigger UpdateCountry on Account (after insert, after update) {
    
    if(Trigger.isInsert){
        Account Acnt = Trigger.new[0];
        Account account = [Select id, Calle_envio__c, Ciudad_envio__c, Codigo_Postal_envio__c, Pais_envio__c, Estado_Provincia_envio__c,
                            Calle_facturacion__c, Ciudad_Facturacion__c, Codigo_Postal_facturacion__c, Pais_facturacion__c, Estado_Provincia_facturacion__c from Account where id =:Acnt.Id];
        if(Acnt != null && Acnt.Copiar_datos_facturacion__c == true&& (account.Calle_envio__c == null || account.Ciudad_envio__c == null || account.Codigo_Postal_envio__c == null || account.Pais_envio__c == null || account.Estado_Provincia_envio__c == null)){
                account.Calle_envio__c = Acnt.Calle_facturacion__c;
                account.Ciudad_envio__c = Acnt.Ciudad_Facturacion__c;
                account.Codigo_Postal_envio__c = Acnt.Codigo_Postal_facturacion__c;
                account.Pais_envio__c = Acnt.Pais_facturacion__c;
                account.Estado_Provincia_envio__c = Acnt.Estado_Provincia_facturacion__c;
        }
        update account;
    }
    if(Trigger.isUpdate){
        Account Acnt = Trigger.new[0];
        Account account = [Select id, Calle_envio__c, Ciudad_envio__c, Codigo_Postal_envio__c, Pais_envio__c, Estado_Provincia_envio__c,Copiar_datos_facturacion__c,
                            Calle_facturacion__c, Ciudad_Facturacion__c, Codigo_Postal_facturacion__c, Pais_facturacion__c, Estado_Provincia_facturacion__c from Account where id =:Acnt.Id];
            if(account != null && Acnt.Copiar_datos_facturacion__c == true && (account.Calle_envio__c == null || account.Ciudad_envio__c == null || account.Codigo_Postal_envio__c == null || account.Pais_envio__c == null || account.Estado_Provincia_envio__c == null)){
                account.Calle_envio__c = account.Calle_facturacion__c;
                account.Ciudad_envio__c = account.Ciudad_Facturacion__c;
                account.Codigo_Postal_envio__c = account.Codigo_Postal_facturacion__c;
                account.Pais_envio__c = account.Pais_facturacion__c;
                account.Estado_Provincia_envio__c = account.Estado_Provincia_facturacion__c;
                update account;
            }
        
    }   
}

 

the code coverage of 85% includes a test class I have succsefully deployed for the previous trigger

 

Test Class	All Tests
Tests Run	5
Test Failures	0
Code Coverage Total %	85
Total Time (ms)	1009.0

 this test class is include in the list of 5

 

@isTest
private class TestTriggerUpdateAccountaddress
    { 
        public static testmethod void testTriggerUpdateAccount()
            {
 Pa_s__c pais = new Pa_s__c(Name = 'TestPais');
 //assign all required values if any
 insert pais;
Provincia__c estado = new Provincia__c(Name='TestEstado');
//assign all required values if any
insert estado;
    
Account acc = new Account();

//Fill all mandatory fields 
acc.Name ='Test';
acc.Origen__c = 'Fitur' ;
acc.Equipo__c = 'Hoteles' ;
acc.Estado__c = 'Activo' ;
acc.Ciudad_envio__c = 'Test' ;
acc.Calle_envio__c = 'Fitur' ;
acc.Codigo_Postal_envio__c = '5454545' ;
acc.Pais_envio__c = pais.id;
acc.Estado_Provincia_envio__c = estado.id;
acc.Tipo_Publicidad__c = 'Proveedores' ;

insert acc;
        }
       }

 

 

But seems not enough!!! Because when I am trying to deploy the trigger I got the error-: 

 

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

 

Before deploying saids 85%, and when i tried to deply the trigger  67%??????

 

Please can you help me to write one more class to reach the 75% of cove  limitation to deply this trigger onto production!!

 

Thanks!!!

 

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

Here is your running test class 

 

@isTest
private class TestTriggerUpdateAccountaddress
    { 
        public static testmethod void testTriggerUpdateAccount()
            {
 Pa_s__c pais = new Pa_s__c(Name = 'TestPais');
 //assign all required values if any
 insert pais;
Provincia__c estado = new Provincia__c(Name='TestEstado');
//assign all required values if any
insert estado;
    
Account acc = new Account();

//Fill all mandatory fields 
acc.Name ='Test';
acc.Origen__c = 'Fitur' ;
acc.Equipo__c = 'Hoteles' ;
acc.Estado__c = 'Activo' ;
acc.Ciudad_envio__c = 'Test' ;
acc.Calle_envio__c = 'Fitur' ;
acc.Pais_envio__c = pais.id;
acc.Estado_Provincia_envio__c = estado.id;
acc.Tipo_Publicidad__c = 'Proveedores' ;
acc.Copiar_datos_facturacion__c = true;

insert acc;
        }

public static testmethod void testTriggerUpdateAccount1()
            {
 Pa_s__c pais = new Pa_s__c(Name = 'TestPais');
 //assign all required values if any
 insert pais;
Provincia__c estado = new Provincia__c(Name='TestEstado');
//assign all required values if any
insert estado;
    
Account acc = new Account();

//Fill all mandatory fields 
acc.Name ='Test';
acc.Origen__c = 'Fitur' ;
acc.Equipo__c = 'Hoteles' ;
acc.Estado__c = 'Activo' ;
acc.Ciudad_envio__c = 'Test' ;
acc.Calle_envio__c = 'Fitur' ;
acc.Pais_envio__c = pais.id;
acc.Estado_Provincia_envio__c = estado.id;
acc.Tipo_Publicidad__c = 'Proveedores' ;
acc.Copiar_datos_facturacion__c = false;

insert acc;
acc.Copiar_datos_facturacion__c = true;
acc.Ciudad_envio__c = 'TestUpdate' ;
update acc;
        }
       }

 

All Answers

My OwnMy Own

It seems like your prod organiztion is not having enough data to validate your code coverage. 

Try to create the account records in test code, in such a way that it will satisfy all the code blocks. 

 

 

DeptonDepton

Can you please help me a bit more with this??

 

I have more than 5000 contacts and 2000 accounts isn´t that ebough?

Also my triggers are simple.

 

How should i do this? How should I proceed?

 

Thank you!!:)

Shashikant SharmaShashikant Sharma
@isTest
private class TestTriggerUpdateAccountaddress
    { 
        public static testmethod void testTriggerUpdateAccount()
            {
 Pa_s__c pais = new Pa_s__c(Name = 'TestPais');
 //assign all required values if any
 insert pais;
Provincia__c estado = new Provincia__c(Name='TestEstado');
//assign all required values if any
insert estado;
    
Account acc = new Account();

//Fill all mandatory fields 
acc.Name ='Test';
acc.Origen__c = 'Fitur' ;
acc.Equipo__c = 'Hoteles' ;
acc.Estado__c = 'Activo' ;
acc.Ciudad_envio__c = 'Test' ;
acc.Calle_envio__c = 'Fitur' ;
acc.Pais_envio__c = pais.id;
acc.Estado_Provincia_envio__c = estado.id;
acc.Tipo_Publicidad__c = 'Proveedores' ;
acc.Copiar_datos_facturacion__c = true;

insert acc;
        }

public static testmethod void testTriggerUpdateAccount1()
            {
 Pa_s__c pais = new Pa_s__c(Name = 'TestPais');
 //assign all required values if any
 insert pais;
Provincia__c estado = new Provincia__c(Name='TestEstado');
//assign all required values if any
insert estado;
    
Account acc = new Account();

//Fill all mandatory fields 
acc.Name ='Test';
acc.Origen__c = 'Fitur' ;
acc.Equipo__c = 'Hoteles' ;
acc.Estado__c = 'Activo' ;
acc.Ciudad_envio__c = 'Test' ;
acc.Calle_envio__c = 'Fitur' ;
acc.Pais_envio__c = pais.id;
acc.Estado_Provincia_envio__c = estado.id;
acc.Tipo_Publicidad__c = 'Proveedores' ;
acc.Copiar_datos_facturacion__c = true;

insert acc;
acc.Ciudad_envio__c = 'TestUpdate' ;
update acc;
        }
       }

 Try above I have updated your method as well as created a new Method also

 

I hope will help you.

DeptonDepton

Thank you man !! 

 

I have tried and got this error deplyoing it

 

Failure Message: "System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateCountry: execution of AfterInsert caused by: System.DmlException: Update failed. First exception on row 0 with id 0013000000p8LnDAAU; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateC...

 

lines 27 and 53 = insert acc;

 

Also I have run the apex test and got 2 errors

Shashikant SharmaShashikant Sharma

Here is your running test class 

 

@isTest
private class TestTriggerUpdateAccountaddress
    { 
        public static testmethod void testTriggerUpdateAccount()
            {
 Pa_s__c pais = new Pa_s__c(Name = 'TestPais');
 //assign all required values if any
 insert pais;
Provincia__c estado = new Provincia__c(Name='TestEstado');
//assign all required values if any
insert estado;
    
Account acc = new Account();

//Fill all mandatory fields 
acc.Name ='Test';
acc.Origen__c = 'Fitur' ;
acc.Equipo__c = 'Hoteles' ;
acc.Estado__c = 'Activo' ;
acc.Ciudad_envio__c = 'Test' ;
acc.Calle_envio__c = 'Fitur' ;
acc.Pais_envio__c = pais.id;
acc.Estado_Provincia_envio__c = estado.id;
acc.Tipo_Publicidad__c = 'Proveedores' ;
acc.Copiar_datos_facturacion__c = true;

insert acc;
        }

public static testmethod void testTriggerUpdateAccount1()
            {
 Pa_s__c pais = new Pa_s__c(Name = 'TestPais');
 //assign all required values if any
 insert pais;
Provincia__c estado = new Provincia__c(Name='TestEstado');
//assign all required values if any
insert estado;
    
Account acc = new Account();

//Fill all mandatory fields 
acc.Name ='Test';
acc.Origen__c = 'Fitur' ;
acc.Equipo__c = 'Hoteles' ;
acc.Estado__c = 'Activo' ;
acc.Ciudad_envio__c = 'Test' ;
acc.Calle_envio__c = 'Fitur' ;
acc.Pais_envio__c = pais.id;
acc.Estado_Provincia_envio__c = estado.id;
acc.Tipo_Publicidad__c = 'Proveedores' ;
acc.Copiar_datos_facturacion__c = false;

insert acc;
acc.Copiar_datos_facturacion__c = true;
acc.Ciudad_envio__c = 'TestUpdate' ;
update acc;
        }
       }

 

This was selected as the best answer
DeptonDepton

:)))))))))))