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
LosintikfosLosintikfos 

1% Coverage

Hi Experts,

I have this trigger;
Code:
trigger addParent on Account (before insert, before update) {
 addParent.addAddress(trigger.new);
}

 I also have this apex class which is invoked by the trigger;
 
Code:
public class addParent{

 public static void addAddress(Account[] acc)
   {
    /**Set Set variables to hold the result as class members**/
    String parent;
    String pCode;
  
        parent = acc[0].ParentId;
           
         for (Account ac : [select Billing PostalCode where Id = :parent]) {
               pCode = ac.BillingPostalCode;
              }
              acc[0].BillingPostalCode = pCode;
          }
     }

 I am using this test coverage below;
 
Code:
public class testAddParentAddress {
    
    public static testMethod void testAddParent(){
    List<Account> accl = new Account[]{new Account()};
    accl[0] = new Account(Name = 'test', ParentId = '001R000000955CDIAY', BillingState = 'MASS', BillingCity = 'Tazmania',    BillingCountry = 'AUS', BillingPostalCode = 'MJ 25355', BillingStreet ='St. Kilda');
  
    String  pCode = accl[0].BillingPostalCode;
 String Street= accl[0].BillingStreet;
 
 System.assertEquals('MJ 25355', pCode);
 System.assertEquals('St. Kilda', Street);
 
    addParent.addAddress(accl);
      
     for (Account o : [select BillingState, BillingCity, BillingCountry, BillingPostalCode, BillingStreet from Account where Name = 'Test'])
     {
      o.BillingState= 'UK' ;
      update o;
     }
    }

}

 Unfurtunately i get error;
 Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required

  Can anyone help to resolve this?





XactiumBenXactiumBen
You need to run the trigger, instead of calling the addAddress method.

To run the trigger you will need to either insert a new record, or update an existing record.  In your case, since you created an account called test you will need to use:

Code:
insert accl;

 Instead of:
Code:
addParent.addAddress(accl);

 
This should get your code coverage to a higher percentage.
LosintikfosLosintikfos
Thanks xactiumBen!


In the end i have to use the two to gain total coverage. Did something like this;
Code:
public class testAddParentAddress {
    
    public static testMethod void testAddParent(){
     List<Account> accl = new Account[]{new Account()};
     accl[0] = new Account(Name = 'test', ParentId = '001R000000955CDIAY', BillingState = 'MASS', BillingCity = 'Tazmania',    BillingCountry = 'AUS', BillingPostalCode = 'MJ 25355', BillingStreet ='St. Kilda'); 
     
     insert accl;
     addParent.addAddress(accl);

}