0

I get this error message when I try to deoploy via change set:

"Code Coverage Failure The following triggers have 0% code coverage. Each trigger must have at least 1% code coverage. PullProjectNoOpportunity –" I thought writing the trigger was the hard part... hah! This trigger populates a lookup field on a child object based on user input into another field on the child object.

Trigger:

trigger PullProjectNoOpportunity on SAP_Sales_Order__c (before update) {
    Set<String> codes = new Set<String>();
    for (SAP_Sales_Order__c o : trigger.new) codes.add(o.SAP_Project_NO__c);

    Map<String, Opportunity> pronumOpportunities = new Map<String, Opportunity>();
    for (Opportunity a : [SELECT Cilioproject__c FROM Opportunity WHERE Cilioproject__c IN :codes]) {
        pronumOpportunities.put(a.Cilioproject__c, a);
    }

    for (SAP_Sales_Order__c o : Trigger.new) {
        Opportunity pronumOpportunity = pronumOpportunities.get(o.SAP_Project_NO__c);
        Id parentId = (pronumOpportunity == null) ? null : pronumOpportunity.Id;
        o.project__c = parentId;
    }
}

I'm following some tutorials on apex, and I thought i just have to create a record that will cause the trigger to fire, so I wrote this for the test class:

@isTest
private class testPullProjectNoOpportunity {
    static testMethod void validateLookupPopulation() {
        SAP_Sales_Order__c SO = new SAP_Sales_Order__c(name='9999999');
        SO.SAP_Project_No__c = '30157';
        insert SO;  
    }
}

But, the test class doesn't test my trigger. I'm thinking that I need to have some code in the test class that evaluates whether the trigger works, but I'm in over my head. Any comments or pointers would be greatly appreciated

EDIT 1: I made the change suggested below so the test class now reads:

@isTest
private class testPullProjectNoOpportunity {
    static testMethod void validateLookupPopulation() {
        SAP_Sales_Order__c SO = new SAP_Sales_Order__c(name='9999999');
        insert SO; 
        SO.SAP_Project_No__c = '30157';
        update SO;
    }
}

But it's still not testing when i try to deploy.

6
  • How do you know its not testing? Aside from the fact that there are no system assert statement to ensure the values on the record were properly update, how do you know it did not test? Did you get a message, if so, please add that to your question verbatim. If you provide some more detail then it may no longer be a duplicate
    – Eric
    Aug 24, 2017 at 2:11
  • When I deploy via change set it get the following: Code Coverage Failure The following triggers have 0% code coverage. Each trigger must have at least 1% code coverage. PullProjectNoOpportunity Aug 24, 2017 at 13:08
  • Put that in your question as it changes the possible answers . That indicates that either the deployment does not contain the test class or it is not being run. Try a changeset.
    – Eric
    Aug 24, 2017 at 13:21
  • Reopened as with the update to include the error message it is now about the test class not being ran on deployment and not how to write a test method
    – Eric
    Aug 24, 2017 at 15:20
  • Can you show us an image of the contents of the changeset. Also when you run the test in sandbox what %coverage does it return?
    – Eric
    Aug 24, 2017 at 15:20

1 Answer 1

2

As you created a before update trigger, you need to update some fields in the created object (after the insert SO;) and then update it using update SO;. Then your trigger will be invoked and you can add assertions to your test method so you check if everything works as desired.

So for example add this after insert SO;:

SO.SAP_Project_No__c = '12345';
update SO;
3
  • First, thank you so much for responding. I made the change you suggested, but, it's still not testing when I try to deploy. Aug 23, 2017 at 21:40
  • Did you also add the testclass to your changeset? Is the trigger correctly invoked when running the test manually? Aug 23, 2017 at 21:53
  • I am using eclipse, and I thought it would run all tests when i go to deploy to production? I'm not sure how to tell if the trigger is correctly invoked when running the test manually. Aug 23, 2017 at 22:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .