0

I wrote a trigger that works perfectly in the Sandbox, and seems to test ok (no errors when running test), but I can't deploy because the Force.com IDE says "Test coverage of selected Apex Trigger is 0%.." What am i missing? Code below. Appreciate any help! I'm new to all this :) Thanks!

================================ Here's my trigger code:

trigger AddNewProducts on Opportunity (after insert) {
    List<BC_Product__c> Childs = new List<BC_Product__c>();
    for(Opportunity a : trigger.new)
    if (a.RecordTypeID == '012600000009MuX')
    {
       BC_Product__c Child = new BC_Product__c ();
       Child.Opportunity__c = a.id;
       Child.Account__c = a.AccountId;
       Child.Product_Description__c = 'HTTP Caching (CDS - Global)'; 
       Child.Provider__c = '001P000000mQ6RM';
       Child.Type__c = 'New';
       Child.Previous_MRC__c = 0;
       Child.Status__c = 'Pending Contract';
       Child.Qty_Units__c = 0;
       Child.Unit_of_Measure__c = 'GB';
       Child.Unit_Price__c = .009;
       Child.Burstable_Rate__c = .009;
       Child.CR_Install_Date__c = date.Today();
       Childs.add(Child);    
       BC_Product__c Child1 = new BC_Product__c ();
       Child1.Opportunity__c = a.id;
       Child1.Account__c = a.AccountId;
       Child1.Product_Description__c = 'HTTPS Secure Asset Delivery (SSL - Global)'; 
       Child1.Provider__c = '001P000000mQ6RM';
       Child1.Type__c = 'New';
       Child1.Previous_MRC__c = 0;
       Child1.Status__c = 'Pending Contract';
       Child1.Qty_Units__c = 0;
       Child1.Unit_of_Measure__c = 'GB';
       Child1.Unit_Price__c = .009;
       Child1.Burstable_Rate__c = .009;
       Child1.CR_Install_Date__c = date.Today();
       Childs.add(Child1);  
       BC_Product__c Child2 = new BC_Product__c ();
       Child2.Opportunity__c = a.id;
       Child2.Account__c = a.AccountId;
       Child2.Product_Description__c = 'Aggregate Commit'; 
       Child2.Provider__c = '001P000000mQ6RM';
       Child2.Type__c = 'New';
       Child2.Previous_MRC__c = 0;
       Child2.Status__c = 'Pending Contract';
       Child2.Qty_Units__c = 100000;
       Child2.Unit_of_Measure__c = 'GB';
       Child2.Unit_Price__c = .009;
       Child2.Burstable_Rate__c = .009;
       Child2.CR_Install_Date__c = date.Today();
       Childs.add(Child2);         

    }

    insert Childs;
}

=============================== And my test class:

@isTest
public class TestAddNewProducts {
    static testMethod void insertNewOpportunity() {
        Opportunity testOpportunity = new Opportunity(
            RecordTypeID = '012600000009MuX',
            AccountID = '0016000000zKiS7',
            Name = 'Test Opportunity Triggers',
            CloseDate = date.Today(),           
            StageName = '1 - Account Planning',
            Type='New Customer',
            Existing_Customer_Category__c='New Service / Product'
        );
        insert testOpportunity;

Test.startTest();

        update testOpportunity;

        Test.stopTest();
}

}

=================================
  • Are you deploying your test class at the same time as your trigger? – Derek F Aug 19 '16 at 20:26
2

Assuming you're deploying both at the same time, you might be suffering from isolation issues (by default, we're always isolated from live data in unit tests). So, you need to make an account for your opportunity to have access to. As a corollary to this, you need to never hard-code Id values like what you're doing. It's guaranteed to create a mess. Here's a cleaner version of your code:

trigger AddNewProducts on Opportunity (after insert) {
    Id 
        recordTypeId    = Opportunity.SObjectType.getDescribe()
                         .getRecordTypeInfosByName().get('NameofRecordType')
                         .getRecordTypeId(),
        providerId      = [SELECT Id FROM Account WHERE Name = 'Provider'].Id;

    BC_Product__c baseTemplate = new BC_Product__c(
        Provider__c=providerId,         Type__c='New',          Previous_MRC__c=0, 
        Status__c='Pending Contract',   Qty_Units__c=0,                 
        Unit_of_measure__c='GB',        Unit_Price__c = 0.009,  
        Burstable_Rate__c=0.009,        CR_Install_Date__c=Date.today()
    );

    BC_Product__c[] 
        template = new BC_Product__c[] { 
            baseTemplate.clone(), 
            baseTemplate.clone(),
            baseTemplate.clone() },
        children = new BC_Product__c[0];

    for(Opportunity a : trigger.new) {
        if(a.RecordTypeId == recordTypeId) {
            BC_Product__c[] temp = template.deepClone();
            temp[0].Product_Description__c = 'HTTP Caching (CDS - Global)';
            temp[1].Product_Description__c = 'HTTPS Secure Asset Delivery (SSL - Global)';
            temp[2].Product_Description__c = 'Aggregate Commit';
            temp[0].Opportunity__c = temp[1].Opportunity__c = // -->
                temp[2].Opportunity__c = a.Id;
            temp[0].Account__c = temp[1].Account__c = // -->
                temp[2].Account__c = a.AccountId;
            children.addAll(temp);
        }
    }
    insert children;
}

Change "NameOfRecordType" and the query criteria for providerId as necessary.

Then, you can address your unit test:

@isTest class TestAddNewProducts {
    @isTest static void insertNewOpportunity() {
        Account provider = new Account(Name='Provider'),
                client = new Account(Name='Client');
        insert new Account[] { provider, client };

        Opportunity testOpportunity = new Opportunity(
            RecordTypeID = Opportunity.SObjectType.getDescribe()
                           .getRecordTypeInfosByName().get('NameofRecordType')
                           .getRecordTypeId()
            AccountID = client.Id,
            Name = 'Test Opportunity Triggers',
            CloseDate = date.Today(),           
            StageName = '1 - Account Planning',
            Type='New Customer',
            Existing_Customer_Category__c='New Service / Product'
        );
        Test.startTest();
        insert testOpportunity;
        Test.stopTest();
        BC_Product__c[] newProducts = [SELECT Id FROM BC_Product__c];
        System.assertEquals(3, newProducts.size());
        // You should also query the other fields and validate behavior.
    }
}
0

Your trigger is running only for insert

Test.startTest();

    update testOpportunity;

    Test.stopTest();

add Test.startTest(); before you insert testOpportunity

  • The Test.startTest() and Test.stopTest() methods only give you a new set of governor limits (for the things that happen in between startTest and stopTest()). It doesn't really have anything to do with code coverage. – Derek F Aug 19 '16 at 20:24
  • The Sandbox says: Code Coverage 100% (145/145); but Force.com IDE says it is 0%. – Sherrie Aug 19 '16 at 20:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.