0

As the title indicates, trying to deploy this trigger and need to get the Code Coverage prerequisite satisfied.

As I created this Test Class I have no methods, thinking I at least established some methods to test against.

Apex Test Class

@isTest
private class UpdateOpportunityProductMonthsToFulfill{

@testSetup 
static void testOLIUpdate()
{
    // Obtain Apex governor limits and resources for this test
    Test.startTest();

    OpportunityLineItem oli = new OpportunityLineItem(Months_to_Fulfill__c='');
    insert oli;

    QuoteLineItem qli = new QuoteLineItem(OpportunityLineItemId=oli.Id);
    insert qli;

    qli.Year_Served_First__c='2018';
    qli.Months_Served_Y1__c='May;June';
    update qli;

   system.assertEquals(oli.Months_to_Fulfill__c, 'May 2018; June 2018');

    // Release governor limits and resources
    Test.stopTest();
}
}

Apex Trigger

trigger UpdateOpportunityProductMonthsToFulfill on QuoteLineItem (after insert, after update) {
String monthsToFulfill = '';
map<String, String> updateMap = new map<String, String>();

// This is where the text for the Months_To_Fulfill__c field is built
for(QuoteLineItem qli : Trigger.new){
    // To make a spelled-out list of months & years instead of the older "start through end"

    List<String> yearOneMonths = qli.Months_Served_Y1__c.replaceAll('None(;)?', '').split(';');
    for(String month : yearOneMonths){
        monthsToFulfill += ( monthsToFulfill.length()==0 ? '' : '; ' ) + month + ' ' + qli.Year_Served_First__c;
    }
    if(!(qli.Year_Served_Second__c == null || qli.Year_Served_Second__c.equals('None'))){
        List<String> yearTwoMonths = qli.Months_Served_Y2__c.replaceAll('None(;)?', '').split(';');
        for(String month : yearTwoMonths){
            monthsToFulfill += '; ' + month + ' ' + qli.Year_Served_Second__c;
        }
    }

    updateMap.put(qli.OpportunityLineItemId, monthsToFulfill);
}

for(String oliID : updateMap.keySet()){
    // This is the calculated value we saved from looping through the Trigger.new Collection
    monthsToFulfill = updateMap.get(oliID);

    // Get the OpportunityLineItems associated with this QuoteId
    List<OpportunityLineItem> oliList = [Select Id, OpportunityId, Months_To_Fulfill__c 
                                         From OpportunityLineItem Where Id  = :oliID ];

    List<OpportunityLineItem> olisToUpdate = new List<OpportunityLineItem>();

    if(oliList.size() > 0){
        for (OpportunityLineItem oli : oliList){
            // Populate this olisToUpdate list because we may 
            // want to get smarter about what's actually updated 
            // in the future.  For now, everything is updated.
            oli.Months_To_Fulfill__c = monthsToFulfill;
            olisToUpdate.add(oli);
        }

        // Batch update the OpportunityLineItems we identified earlier
        if(olisToUpdate.size() > 0){
            update olisToUpdate;
        }
    }
}     
}
1
  • you should use @ isTest instead of @ testsetup
    – Himanshu
    Commented Mar 30, 2018 at 15:33

1 Answer 1

2

There's an important note buried at the bottom of the documentation on @testSetup:

If a test setup method calls a non-test method of another class, no code coverage is calculated for the non-test method.

This method doesn't need to be @testSetup, though - it's not performing setup work. Just change the annotation to @isTest.

While your test class will not catch the resulting LimitsException, you do have a bulkification issue in your trigger. You need to move your SOQL query outside of the loop or you'll get exceptions on bulk operations.

6
  • Thank you @David Reed for the follow up. I still after removing the testSetup, I still have no Test Methods? I feel I am missing something simple. As for the SOQL query, I am going to change accordingly.
    – Kyle Golik
    Commented Mar 30, 2018 at 17:05
  • 1
    @KyleGolik You need to change @testSetup to @isTest.
    – sfdcfox
    Commented Mar 30, 2018 at 17:26
  • This is the beginning of my code, when I put in a second isTest, it errors out: code@isTest private class UpdateOpportunityProductMonthsToFulfill{ static void testOLIUpdate() { // Obtain Apex governor limits and resources for this test Test.startTest();
    – Kyle Golik
    Commented Mar 30, 2018 at 17:33
  • Your test method must also be annotated with @isTest. See the example of a complete test class in the link in the answer above.
    – David Reed
    Commented Mar 30, 2018 at 17:37
  • Thank you @DavidReed now I got a basic syntax question, when I Run Test on my Test Class I get this error: "System.AssertException: Assertion Failed: Expected: , Actual: May 2018" This is the snippet of my code: code qli.Year_Served_First__c='2018'; qli.Months_Served_Y1__c='May'; update qli; system.assertEquals(oli.Months_to_Fulfill__c, 'May 2018'); // Release governor limits and resources Test.stopTest(); } }code
    – Kyle Golik
    Commented Mar 30, 2018 at 19:39

You must log in to answer this question.

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