+ Start a Discussion
ckellieckellie 

How do I solve this test class error?

I am trying test the apex trigger below:

trigger IndustrytoAcctOpp on Customer_Product_Line_Item__c (Before Insert, Before Update) {

    Map<string, string> imap = new Map<string, string>();
   
    for(Industry_Definition__c i : [select id, Market_Segment__c, Strategic_Industry__c from Industry_Definition__c]){
        imap.put(i.Market_Segment__c, i.strategic_industry__c);
        system.debug('##########i.Market_segment__c:'+ i.market_segment__c);
    }
        
    Map<Id,Customer_Product_Line_Item__c> map_AccountID_CPLI = new Map<Id,Customer_Product_Line_Item__c>();
    Map<Id,Customer_Product_Line_Item__c> mapOpportunity = new Map<Id,Customer_Product_Line_Item__c>();
    Map<Id,Customer_Product_Line_Item__c> mapCPS = new Map<Id,Customer_Product_Line_Item__c>();
    List<Account> list_AccountsToUpdate = new List<Account>();
                     
    for ( Customer_Product_Line_Item__c cpli : trigger.new ) {
         cpli.Strategic_Industry__c = imap.get( cpli.Industry_Segment__c );
         map_AccountID_CPLI.put( cpli.Account__c, cpli );
         mapOpportunity.put(cpli.Opportunity__c, cpli);

         }

    For(Account a : [select id, Strategic_Industrycp__c, Market_Segment__c from account where id in: map_AccountID_CPLI.keySet()]){
        a.strategic_Industrycp__c = map_AccountID_CPLI.get( a.Id ).Strategic_Industry__c;
        if(a.market_segment__c.contains(map_AccountID_CPLI.get( a.Id ).Industry_Segment__c)){
        } else if(a.market_segment__c.contains(map_AccountID_CPLI.get( a.Id ).Industry_Segment__c)){
        } else{
        a.Market_Segment__c = a.market_segment__c + '; '+(map_AccountID_CPLI.get( a.Id ).Industry_Segment__c);
        }
        list_AccountsToUpdate.add( a );   
    }
    list_AccountsToUpdate.sort();
    update list_AccountstoUpdate;
   
    List<Opportunity> list_OpportunitiesToUpdate = new List<Opportunity>();
   
    For(Opportunity o : [select id, Strategic_Industry__c, Industry_Segment__c from Opportunity where id in: mapOpportunity.keySet()]){
    String existingsegments = o.Industry_Segment__c;
    string newSegment =mapOpportunity.get(o.id).Industry_Segment__c;
   
        o.strategic_Industry__c = mapOpportunity.get( o.Id ).Strategic_Industry__c;
        o.Industry_Segment__c = mapOpportunity.get( o.Id ).Industry_Segment__c;
     if(existingSegments == null){
         o.Industry_Segment_text__c = mapOpportunity.get( o.Id ).Industry_Segment__c;
     }else if (existingSegments.contains(mapOpportunity.get( o.Id ).Industry_Segment__c)== true){
         o.Industry_Segment_text__c = existingSegments;
     }else if (existingSegments.contains(mapOpportunity.get( o.Id ).Industry_Segment__c)==false ){
          o.Industry_Segment_text__c =  o.Industry_Segment_text__c + ', ' + mapOpportunity.get( o.Id ).Industry_Segment__c;
     }
   
        list_OpportunitiesToUpdate.add( o ); 
    }
    update list_OpportunitiesToUpdate;

}
This is my test class:


@isTest
private class TestIndustrytoAcctOpp{
     testmethod private static void TestTrigger() {
        Opportunity opt = new Opportunity();
       
    //Setup User
    User u1 = new user (Username = ' test@key.net',
                        alias='test',
                        LastName ='test',
                        email='test@key.net',
                        communityNickname='test',
                        TimeZoneSidKey='America/Los_Angeles',
                        LocaleSidKey='en_US',
                        EmailEncodingKey='ISO-8859-1',
                        ProfileId='00e30000000gAkF',
                        LanguageLocaleKey='en_US' );
    insert u1;
         System.runAs(u1) {
         // The following code runs as user 'u1' 
   
         System.debug('Current User: ' + UserInfo.getUserName());
         System.debug('Current Profile: ' + UserInfo.getProfileId()); }

    u1 = [select id from user where alias = 'test'];
   
   Test.startTest();
        Account a = new account ( name = 'accounttest', type = 'customer');
        Insert a;
       
        Opportunity o = new Opportunity (Recordtypeid = '012300000000Vnm',
                                            Name = 'opportunitytest',
                                            account=a,
                                            stagename = 'Budget',
                                            CloseDate = date.parse('1/1/2020'),
                                            Amount = 5.00,
                                            INCO_Terms__c = 'FCA',
                                            Related_Location__c = 'Walla Walla',
                                            Probability_Key_Will_Win_Order__c = 0.01,
                                            ForecastCategoryName = 'Pipeline',
                                            Equip_Services_Options_to_be_quoted__c = 'tegra',
                                            Op_Market_Position__c = '1 - Existing Equip - Existing App/Market (PB items a known app and current cust base)',
                                            Product_1__c = 'apple',
                                            Product1_Condition__c = 'raw',
                                            Customer_Products_Attached__c = null,
                                            Product1_Line_Capacity__c = '20000 lbs/hr');
        Insert o;
       
        Customer_Product__c c = new Customer_Product__c (name='apple');
        insert c;
       
        Customer_Product_Line_Item__c p = new Customer_Product_Line_Item__c(
                                        Customer_Product__c = c.id,
                                        Account__c = a.id,
                                        Opportunity__c = o.id,
                                        Condition_1__c = 'wet',
                                        Shape_1__c = 'whole',
                                        Capacity__c = '1',
                                        Industry_Segment__c = 'Apples',
                                        Strategic_Industry__c = 'Fresh Processed Fruit & Vegetable');
        insert p;
                    o.Strategic_Industry__c = 'Fresh Processed Fruit & Vegetable';
                    update o;
        Test.stopTest();
       
}
}

How do I test aupdatingthe market segment to the account on line 25?

Here is tyhe line of code:if(a.market_segment__c.contains(map_AccountID_CPLI.get( a.Id ).Industry_Segment__c))

Thank you
GunnarGunnar
Somewhere in the latter part of the test, do a select of the account and read the value.