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
jevelethjeveleth 

Apex trigger and code coverage

I'm trying to create an Apex trigger that will prevent a record on the custom object Work Order from being deleted if a box in the object (Ready For Invoice) is checked. The trigger fires in my sandbox, but my unit tests seem to cover 0% of the trigger. I've walked through the relevant sections of the Force.com workbook on testing (Tutorials 7 and 8), but I'm having little luck. Can anyone advise?


The code is below:

 

//Trigger
trigger noDeleteReadyForInvoice on Work_Order__c (before delete)
  {
     for (Work_Order__c q: trigger.old)
      if (q.Ready_for_Invoice__c == true)
      {
       q.AddError('Cannot delete Work Order that is ready for invoice.');
       }
  }

//Test
@isTest
private class noDeleteReadyForInvoice {
static testMethod void testWorkOrderTrigger(){

//First, prepare 200 work orders for the test data
Account a = new Account(Name='test account');
insert a;
Projects__c p = new Projects__c(Account__c=a.Id,Project_Name__c='test project');
insert p;

Work_Order__c[] wo = new Work_Order__c[]{
new Work_Order__c(Project__c=p.Id,Work_Order_Description__c='test description',Ready_for_Invoice__c=true)
};
insert wo;

//Now insert data causing a work order trigger to fire. 

Test.startTest();
update wo;
Test.stopTest();

 

Update:

 

I've change "update wo" to "delete wo" in the test, but the trigger's code still doesn't seem to be covered: http://i.imgur.com/GGsb2.png. 

Best Answer chosen by Admin (Salesforce Developers) 
jevelethjeveleth

Update. The test below works. Thanks for the assistance.

 

/**
This test class tests a deletion trigger.
**/
@isTest private class noDeleteReadyForInvoice { static testMethod void testWorkOrderTrigger() { // Obtain Apex governor limits and resources for this test Test.startTest(); //Create fake parent account, project, and work order records Account a = new Account(Name='test account'); insert a; Projects__c p = new Projects__c(Account__c=a.Id,Project_Name__c='test project'); insert p; Work_Order__c wo = new Work_Order__c(Project__c=p.Id,Work_Order_Description__c='test description'); // Now save work order to database. insert wo; // Simulate check ready to invoice box and save the change wo.Ready_for_Invoice__c = true; update wo; // Now attempting to delete the record which *should* throw an exception // which we will then catch try { delete wo; } catch (System.Dmlexception e) { } // And then assert that the work order record has been deleted (null) System.assertNotEquals(wo, null); // Release governor limits and resources Test.stopTest(); } }

 

 

 

All Answers

kevoharakevohara

Your trigger uses "before delete" only but your test is running an update, not a delete.

 

You need to run

 

Test.startTest();

delete wo;

Test.stopTest();

 

jevelethjeveleth

Kevin,

 

Thanks, but the code still doesn't seem to be covered: http://i.imgur.com/GGsb2.png even after I change update to delete. 

 

The message in the linked image points to the first line of code in the trigger. The message reads as follows:

 

"Multiple markers at this line:

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

- File only saved locally, not to server."

 

When I attempt to save the file to the server, I get a message that my trigger is "dirty." That said, even after I've saved the trigger to the server, I continue to get the red cross.

kevoharakevohara

The coverage window at the bottom shows 100% coverage.  I think you are good, you mught just have a dirty file.  Are you editing in both the IDE and the Salesforce UI?  That can cause issues.

 

Do this...

 

1)  In the IDE, right-click the src folder and select "Refresh from Server"

2)  Run your tests again.

 

 

jevelethjeveleth

Thanks, Kevin. Alas, I now get the following failure. Any notion what I've done wrong?

 

*** Deployment Log ***
Result: FAILED
Date: September 19, 2011 2:21:57 PM PDT

# Deployed From:
   Project name: Production
   Username: josh@crowdflower.com
   Endpoint: www.salesforce.com

# Deployed To:
   Username: josh@crowdflower.com
   Endpoint: www.salesforce.com

# Deploy Results:
   File Name:    package.xml
   Full Name:  package.xml
   Action:  UPDATED
   Result:  SUCCESS
   Problem: n/a

   File Name:    triggers/noDeleteReadyForInvoice.trigger
   Full Name:  noDeleteReadyForInvoice
   Action:  UPDATED
   Result:  SUCCESS
   Problem: n/a

# Test Results:

Run Failures:
  noDeleteReadyForInvoice.testWorkOrderTrigger System.DmlException: Delete failed. First exception on row 0 with id a09A000000J6C3kIAF; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Cannot delete Work Order that is ready for invoice.: []

 

 

 

ashish raiashish rai

Hello,

     Use this:

 

@isTest
private class noDeleteReadyForInvoice
{
    static testMethod void testWorkOrderTrigger()
    {
    Account a = new Account(Name='test account');
    insert a;
    Projects__c p = new Projects__c(Account__c=a.Id,Project_Name__c='test project');
    insert p;
    Work_Order__c wo = new Work_Order__c(Project__c=p.Id,Work_Order_Description__c='test description',Ready_for_Invoice__c=true);   
    insert wo;
    delete wo;   
    }
}

 

Think this will help you to cover.

jevelethjeveleth

Thanks, but I still get the following error.

*** Deployment Log ***
Result: FAILED
Date: September 20, 2011 7:53:58 AM PDT

# Deployed From:
   Project name: Production
   Username: josh@crowdflower.com
   Endpoint: www.salesforce.com

# Deployed To:
   Username: josh@crowdflower.com
   Endpoint: www.salesforce.com

# Deploy Results:
   File Name:    classes/noDeleteReadyForInvoice.cls
   Full Name:  noDeleteReadyForInvoice
   Action:  NO ACTION
   Result:  SUCCESS
   Problem: n/a

   File Name:    package.xml
   Full Name:  package.xml
   Action:  UPDATED
   Result:  SUCCESS
   Problem: n/a

   File Name:    triggers/noDeleteReadyForInvoice.trigger
   Full Name:  noDeleteReadyForInvoice
   Action:  UPDATED
   Result:  SUCCESS
   Problem: n/a

# Test Results:

Run Failures:
  noDeleteReadyForInvoice.testWorkOrderTrigger System.DmlException: Delete failed. First exception on row 0 with id a09A000000JF6VxIAL; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Cannot delete Work Order that is ready for invoice.: []

 

jevelethjeveleth

Update. The test below works. Thanks for the assistance.

 

/**
This test class tests a deletion trigger.
**/
@isTest private class noDeleteReadyForInvoice { static testMethod void testWorkOrderTrigger() { // Obtain Apex governor limits and resources for this test Test.startTest(); //Create fake parent account, project, and work order records Account a = new Account(Name='test account'); insert a; Projects__c p = new Projects__c(Account__c=a.Id,Project_Name__c='test project'); insert p; Work_Order__c wo = new Work_Order__c(Project__c=p.Id,Work_Order_Description__c='test description'); // Now save work order to database. insert wo; // Simulate check ready to invoice box and save the change wo.Ready_for_Invoice__c = true; update wo; // Now attempting to delete the record which *should* throw an exception // which we will then catch try { delete wo; } catch (System.Dmlexception e) { } // And then assert that the work order record has been deleted (null) System.assertNotEquals(wo, null); // Release governor limits and resources Test.stopTest(); } }

 

 

 

This was selected as the best answer