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
DT DeveloperDT Developer 

Need help with Test Method

First time creating a Apex Class  and the Trigger and I'm getting a error message:

Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required

I have 100% coverage for the Apex Class but can't find any information on getting Apex Trigger Coverage.

Any help is appreciated

Class:

public class ContractUpdate {

public static void AddeCon(Contract [] UpdateContract) {
    List<Account> accountsToUpdate = new List<Account>();
    for(Contract c: UpdateContract)
   
{
    if ((c.Stage__c=='Completed') && (c.RecordTypeId == '0123000000002Hr') && (c.Status == 'Active')){
   Account acc = new Account(Id=c.AccountId,eCon_Completed__c=TRUE);
   accountsToUpdate.add(acc);
  }
 else { Account acc = new Account(Id=c.AccountId,eCon_Completed__c=FALSE);
     accountsToUpdate.add(acc);
 }
 }
 update accountsToUpdate;
}


    public static testMethod void test_AddeCon(){
   

      Account test1 = new Account(Name = 'one', eCon_Completed__c=FALSE);   
      Account test2 = new Account(Name = 'two',eCon_Completed__c=TRUE);
      Account test3 = new Account(Name = 'three',eCon_Completed__c=FALSE);
      Account test4 = new Account(Name = 'four',eCon_Completed__c=TRUE);
      Account[] accts = new Account[] { test1, test2, test3, test4 };
          
       insert accts;
     
      Contract TestCon1 = new Contract (AccountId = test1.Id, Stage__c = 'Completed',Status = 'Active',RecordTypeId = '0123000000002Hr');
      Contract TestCon2 = new Contract (AccountId = test2.Id, Stage__c = 'Install on Hold', Status = 'Active');
      Contract TestCon3 = new Contract (AccountId = test3.Id, Stage__c = 'Completed', Status = 'Cancelled', RecordTypeId = '0123000000002Hr');
      Contract TestCon4 = new Contract (AccountId = test4.Id, Stage__c = 'Completed',Status = 'Active', RecordTypeId = '0123000000002Hr');
      Contract[] con = new Contract[] {TestCon1, TestCon2, TestCon3, TestCon4};
     
      AddeCon(con);
          
      // Execute trigger with test data set
    
      // Confirm results
      Account[] acctQuery = [SELECT eCon_Completed__c FROM Account WHERE Id = :accts[0].Id OR Id = :accts[1].Id OR Id = :accts[2].Id OR Id = :accts[3].Id];
      System.assertEquals(TRUE, acctQuery[0].eCon_Completed__c);
      System.assertEquals(FALSE, acctQuery[1].eCon_Completed__c);
      System.assertEquals(FALSE, acctQuery[2].eCon_Completed__c);
      System.assertEquals(TRUE, acctQuery[3].eCon_Completed__c);
   }
  
}

Trigger:

trigger ContractUpdate on Contract (after update) {

ContractUpdate.AddeCon(Trigger.new);
 
}


Message Edited by DT Developer on 09-16-2008 02:32 PM

Message Edited by DT Developer on 09-16-2008 02:43 PM
BritishBoyinDCBritishBoyinDC
Are you sure the test class is actually calling the trigger? That is typically why you get that error, and I am not sure looking at the code below that it is being called by the test. So I would put a debug statement in the trigger to make sure the test class is actually calling it... and if is not, you might need to amend the test script to not only create new contracts, but also update them, which should ensure the trigger is called and tested.
HL-DevHL-Dev
In your test method, change this line:
Code:
AddeCon(con);

 
to an update call:
Code:
update con;

Your trigger is an "After Update" one, so the above call should execute it.