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
KirstyFranceKirstyFrance 

Apex tests have stopped working - Average test coverage has fallen below 75%

A whole lot of tests that were working fine are suddenly producing errors on our production environment meaning the test coverage has fallen below 75% and we can't update anything.

 

While I have found a couple of genuine errors and fixed in the Sandbox ready to deploy, I'm at a loss to understand the majority of them.

 

Example 

System.AssertException: Assertion Failed: Expected: Benchmark, Actual: N/A	Class.testUKItemCodes.testUKItemCodes: line 47, column 5 External entry point

 When I actually create a record on the object that has the trigger which populates the value Benchmark in a text field, it works fine and I don't get N/A. 

 

But for some reason when the test class does the same action virtually, it gets a different value.

 

I'm really at a loss as I they were working fine until recently. 

 

We need to update some of the triggers due to product changes and can't because we can't get the test coverage up.


Any help or ideas would be appreciated.

 

Regards

 

Kirsty - Livebookings

Shashikant SharmaShashikant Sharma

I would like to see two things

1)Your test method which is failing

2)Trigger which sets te benchmark

KirstyFranceKirstyFrance

Hi there,

 

I have actually now got it to work on the Sandbox (although have not tried to deploy yet), with the help of a developer in the office. However neither of us understand why what we changed made it work and why it worked previously and then stopped.

 

An example of a test class that was failing is:

public class testSwedenItemCodes
{
//Swedish Account
    static testMethod void testSwedenItemCodes(){
    Account accountOneSweden = new Account(Name='accountOneSweden', site='accountOneSweden', 
    Prospect_Source_Main_Category__c='website', type='customer', 
    Livebookings_Reservation_Book_s__c='LB Console Full', 
    HomePage_interface__c='Pending',
    Phone='+99 9999999999',
    ShippingCity='London', 
    ShippingCountry='SWE',
    Budget_City__c='London', 
    Market__c='SWE'

    );
    insert accountOneSweden;

//Sverige Barometer Benchmark
    Price_Plan__c PricePlanSweden1 = new Price_Plan__c( Account__c=accountOneSweden.id,
    market__c='SWE',
    Price_Plan__c='Sverige Barometer Benchmark',
    Unit_Type__c='monthly',
    Payment_Period__c='monthly',
    Sales_Price__C=10.00,
    Item_Code__c='1050'
     
      );
    insert PricePlanSweden1;
     
    Price_Plan__c insertedPricePlanSweden1 = [Select Item_Code__c, List_Price__c,Bill_in_advance_Bill_in_arrears__c, Covers_Included__c, Type__c
    from Price_Plan__c where id=:PricePlanSweden1.id
    ];

    System.assertEquals(
            '1050', 
            insertedPricePlanSweden1.Item_Code__c);
    System.assertEquals(
            'In Advance', 
            insertedPricePlanSweden1.Bill_in_advance_Bill_in_arrears__c);
    System.assertEquals(
            100, 
            insertedPricePlanSweden1.List_Price__c);
    System.assertEquals(
            0, 
            insertedPricePlanSweden1.Covers_Included__c);
   System.assertEquals(
      'Benchmark', 
       insertedPricePlanSweden1.Type__c);

 Previously we did not have the line:

Item_Code__c='1050'

 In the price plan creation. We assumed that because the item code is set by trigger we did not need to set it here. Adding this line stopped the test throwing an error.

 

The trigger that sets the value "Benchmark" looks like this:

trigger SetType on Price_Plan__c (before insert,before update) 
{
  for (Price_Plan__c a : Trigger.new) 
    {      
        if(a.Item_Code__c=='1007'||a.Item_Code__c=='1010'||a.Item_Code__c=='1022'||a.Item_Code__c=='1026'||a.Item_Code__c=='1029'||a.Item_Code__c=='1032'|| a.Item_Code__c=='1078'||a.Item_Code__c=='1041'||a.Item_Code__c=='1044'|| a.Item_Code__c=='1047' || a.Item_Code__c=='1060'||a.Item_Code__c=='1061'||a.Item_Code__c=='1087'||a.Item_Code__c=='1091'||a.Item_Code__c=='1093'||a.Item_Code__c=='1134'||a.Item_Code__c=='1165'||a.Item_Code__c=='1170'||a.Item_Code__c=='1176'||a.Item_Code__c=='1177'||a.Item_Code__c=='1178'||a.Item_Code__c=='1179'||a.Item_Code__c=='1180'||a.Item_Code__c=='1181'||a.Item_Code__c=='1182'||a.Item_Code__c=='1183'||a.Item_Code__c=='1184'||a.Item_Code__c=='1185'||a.Item_Code__c=='1186'||a.Item_Code__c=='1193')
        {a.Type__c = 'Light' ;} 

        else if (a.Item_Code__c=='1001'|| a.Item_Code__c=='1002'||a.Item_Code__c=='1015'||a.Item_Code__c=='1016'||a.Item_Code__c=='1035'||a.Item_Code__c=='1036'|| a.Item_Code__c=='1054'||a.Item_Code__c=='1055'||a.Item_Code__c=='1072')
        {a.Type__c = 'Cover Bundle Light' ;} 

        
        else if (a.Item_Code__c=='1013'||a.Item_Code__c=='1009'||a.Item_Code__c=='1021'|| a.Item_Code__c=='1028'|| a.Item_Code__c=='1030'||a.Item_Code__c=='1034'||a.Item_Code__c=='1043'||a.Item_Code__c=='1046'||a.Item_Code__c=='1049'||a.Item_Code__c=='1066'||a.Item_Code__c=='1067'||a.Item_Code__c=='1082'||a.Item_Code__c=='1089'||a.Item_Code__c=='1092'||a.Item_Code__c=='1100'||a.Item_Code__c=='1103'||a.Item_Code__c=='1105'||a.Item_Code__c=='1106'||a.Item_Code__c=='1107'||a.Item_Code__c=='1111'||a.Item_Code__c=='1122'||a.Item_Code__c=='1124'||a.Item_Code__c=='1192'||a.Item_Code__c=='1195')
        {a.Type__c = 'Standard' ;} 

        else if (a.Item_Code__c=='1005'|| a.Item_Code__c=='1006'||a.Item_Code__c=='1019'||a.Item_Code__c=='1020'||a.Item_Code__c=='1039'||a.Item_Code__c=='1040'|| a.Item_Code__c=='1058'||a.Item_Code__c=='1059'||a.Item_Code__c=='1076'|| a.Item_Code__c=='1077'||a.Item_Code__c=='1085'|| a.Item_Code__c=='1086'|| a.Item_Code__c=='1131')
        {a.Type__c = 'Cover Bundle Standard' ;} 

        
        else if (a.Item_Code__c=='1012'||a.Item_Code__c=='1008'||a.Item_Code__c=='1024'|| a.Item_Code__c=='1027'||a.Item_Code__c=='1031'|| a.Item_Code__c=='1022'|| a.Item_Code__c=='1033'||a.Item_Code__c=='1042'||a.Item_Code__c=='1045'||a.Item_Code__c=='1048'||a.Item_Code__c=='1062'||a.Item_Code__c=='1063'||a.Item_Code__c=='1079'||a.Item_Code__c=='1095'||a.Item_Code__c=='1096'||a.Item_Code__c=='1097'||a.Item_Code__c=='1102'||a.Item_Code__c=='1104'||a.Item_Code__c=='1112'||a.Item_Code__c=='1125'||a.Item_Code__c=='1126'||a.Item_Code__c=='1127'||a.Item_Code__c=='1128'||a.Item_Code__c=='1129'||a.Item_Code__c=='1130'||a.Item_Code__c=='1132'||a.Item_Code__c=='1136'||a.Item_Code__c=='1175'||a.Item_Code__c=='1190'||a.Item_Code__c=='1194')
        {a.Type__c = 'Premium' ;} 

        else if (a.Item_Code__c=='1003'|| a.Item_Code__c=='1004'||a.Item_Code__c=='1017'||a.Item_Code__c=='1018'||a.Item_Code__c=='1037'||a.Item_Code__c=='1038'|| a.Item_Code__c=='1056'||a.Item_Code__c=='1057'||a.Item_Code__c=='1074'|| a.Item_Code__c=='1075')
        {a.Type__c = 'Cover Bundle Premium' ;} 
        
        else if(a.Item_Code__c=='1050'||a.Item_Code__c=='1068'||a.Item_Code__c=='1114'||a.Item_Code__c=='1118'|| a.Item_Code__c=='1147'|| a.Item_Code__c=='1151'|| a.Item_Code__c=='1157'|| a.Item_Code__c=='1161'|| a.Item_Code__c=='1166')
        {a.Type__c = 'Benchmark' ;} 
        
        else if(a.Item_Code__c=='1051'||a.Item_Code__c=='1069'||a.Item_Code__c=='1116'||a.Item_Code__c=='1120'||a.Item_Code__c=='1148'||a.Item_Code__c=='1152'||a.Item_Code__c=='1158'|| a.Item_Code__c=='1162'|| a.Item_Code__c=='1167')
        {a.Type__c = 'Extended' ;} 
        
        else if(a.Item_Code__c=='1053'||a.Item_Code__c=='1071'||a.Item_Code__c=='1115'||a.Item_Code__c=='1119'||a.Item_Code__c=='1141'||a.Item_Code__c=='1149'||a.Item_Code__c=='1153'||a.Item_Code__c=='1160'|| a.Item_Code__c=='1164'|| a.Item_Code__c=='1169')
        {a.Type__c = 'Guest Feedback' ;} 
        
        else if(a.Item_Code__c=='1052'||a.Item_Code__c=='1070'||a.Item_Code__c=='1121'||a.Item_Code__c=='1142' ||a.Item_Code__c=='1150'||a.Item_Code__c=='1159'|| a.Item_Code__c=='1163'|| a.Item_Code__c=='1168')
        {a.Type__c = 'Extended + Benchmark' ;} 

        else if(a.Item_Code__c=='1154')
        {a.Type__c = 'Extended + Benchmark + Guest Feedback' ;} 

        else if(a.Item_Code__c=='1145'||a.Item_Code__c=='1146')
        {a.Type__c = 'Connect' ;} 
        
        else if(a.Item_Code__c=='1156')
        {a.Type__c = 'Facebook' ;} 
        
        else if(a.Item_Code__c=='1155'||a.Item_Code__c=='1171'||a.Item_Code__c=='1172'||a.Item_Code__c=='1189')
        {a.Type__c = 'Listings' ;} 

        else if(a.Item_Code__c=='1187'||a.Item_Code__c=='1188'||a.Item_Code__c=='1191'||a.Item_Code__c=='1196'||a.Item_Code__c=='1197'||a.Item_Code__c=='1198'||a.Item_Code__c=='1199'||a.Item_Code__c=='1200'||a.Item_Code__c=='1201')
        {a.Type__c = 'Pocket Diner' ;}
        
        else if(a.Item_Code__c=='1065'||a.Item_Code__c=='1081')
        {a.Type__c = 'SMS' ;} 
        
        else if(a.Item_Code__c=='1064'||a.Item_Code__c=='1080'||a.Item_Code__c=='1098'||a.Item_Code__c=='1099'||a.Item_Code__c=='1128' ||a.Item_Code__c=='1129'||a.Item_Code__c=='1130'|| a.Item_Code__c=='1132')
        {a.Type__c = 'Support' ;} 
        
        else if(a.Item_Code__c=='1011'||a.Item_Code__c=='1014'||a.Item_Code__c=='1023'||a.Item_Code__c=='1025'||a.Item_Code__c=='1090' ||a.Item_Code__c=='1094'||a.Item_Code__c=='1113'|| a.Item_Code__c=='1117'|| a.Item_Code__c=='1135')
        {a.Type__c = 'Other' ;} 

        else if(a.Item_Code__c=='1173'||a.Item_Code__c=='1174')
        {a.Type__c = 'Reports' ;} 
        
        else {a.Type__c = 'N/A' ;}

     }

}

 This when tested on real data works. 

Shashikant SharmaShashikant Sharma

Because you are asserting this

 

    System.assertEquals(
            '1050', 
            insertedPricePlanSweden1.Item_Code__c);

 And if you will not insert it  then this query will  give you Item_Code__c as null

 

Price_Plan__c insertedPricePlanSweden1 = [Select Item_Code__c, List_Price__c,Bill_in_advance_Bill_in_arrears__c, Covers_Included__c, Type__c
    from Price_Plan__c where id=:PricePlanSweden1.id
    ];

 

And so your assertion will fail.

 

 

Adam PurkissAdam Purkiss

Reading through this issue I'm unclear on how exactly it was *ever* working, without specifying '1050' for Item_Code__c.

 

A few thoughts about data in general. In the event that a space was unexpectedly added to any of your codes, assuming the length of Item_Code__c is > 4, then any assertion about it equaling '1050' would fail since it might = '1050 '.In the event that this is possible, adding the .trim() would do the trick. But from a code maintenance and readability perspective, I'd consider refactoring away from hundreds of literals in conditional statements.

 

Using a list of codes, or lists of codes that each represent a return type, along with a method to evaluate a specific Item_Code__c value against, would be cleaner - imo.

 

 

KirstyFranceKirstyFrance

Hi All,

 

Thanks for the advice. I'm not a developer, so I've cobbled it together as best I can. It was definitely working though. I think I did not specifiy the item code on the insert as I assumed the trigger would insert it.

 

Regards

Kirsty