3

In the Apex class below (executed in a client org), testA() passes while testB() fails. The failure is an assertion error from the ServiceLayer constructor.

/*  SF API 33  */
/*  PKG version 1.55  */
@isTest private class TestClass
{
    static testmethod void testA()
    {
        // setup test data A

        // call method that gets fields dynamically
        ServiceLayer l = new ServiceLayer();
    }

    static testmethod void testB()
    {
        // setup test data B

        // call method that gets fields dynamically
        ServiceLayer l = new ServiceLayer();
    }
} 

/*  SF API 33  */
/*  PKG version 1.55  */
global class ServiceLayer
{
    global ServiceLayer()
    {
        // get fields dynamically
        Set<String> fields = PKG.UtilityClass.getFields('MY__Object__c');
        System.assert(fields.contains('Name'));
    }
}

Code in the package PKG:

/*  SF API 27  */
global class UtilityClass
{
    global static Set<String> getFields(String sobjType)
    {
        // use basic describe calls to get SObject fields -no magic

        return fields;
    }
}

Basically when testB() runs the PKG code returns less fields than there are on the object (Name is one of them). But when the PKG code is called in Developer Console it returns the correct results. We also get no issues when the code utilizing ServiceLayer is executed as part of its business use-cases.

Now brace for the ridiculous punchline.

If I call the PKG code inside testB() the assertion passes.

    static testmethod void testB()
    {
        Set<String> fields = PKG.UtilityClass.getFields('MY__Object__c');
        System.assert(fields.contains('Name')); // passes 

        // same as before
        // assertion from ServiceLayer still fails
    }

Is there even an explanation to this? I'd be okay passing it as a mysterious issue with no answers.

UPDATE: specified the PKG version for the classes (they're all the same version).

  • Using asserts in production code smells of bad practice. Is that something you can remove? – Adrian Larson Jul 1 '15 at 4:24
  • It was added as part of debugging only. – Mossi Jul 1 '15 at 20:23
2

I have faced the similar Issue, Your package class runs on API version 27.0 and rest all classes runs on 33.0, So There are things which affects your due to API change:-

1. getGlobalDescribe() implementation change(API version 28.0):-

Starting with Apex saved using Salesforce.com API version 28.0, the keys in the map that getGlobalDescribe returns are always prefixed with the namespace, if any, of the code in which it is running. For example, if the code block that makes the getGlobalDescribe() call is in namespace ffm, and a custom object named BillingLineGroup__c is in the same namespace, the key returned is ffm_BillingLineGroup__c. For Apex saved using earlier API versions, the key contains the namespace only if the namespace of the code block and the namespace of the sObject are different. For example, if the code block that generates the map is in namespace ffm, and an sObject is also in ffm, the key in the map is represented as BillingLineGroup__c In Order to incorporate this change we have to add namespace prefix with the object name wherever we are retrieving object data from global description.

For Example:-- Use private static Schema.SObjectType ffBillingLineGroupType = s_mapGlobalDescribe.get(‘ffm_’+'BillingLineGroup__c'); Instead of private static Schema.SObjectType ffBillingLineGroupType = s_mapGlobalDescribe.get('BillingLineGroup__c');

2. There are two fields newly added in API version 28.0

LastViewedDate
LastReferencedDate

3. Developer console always runs with the latest API Version

|improve this answer|||||
  • I am aware of those changes, and unfortunately none of them explain the inconsistency mentioned in the punchline. a) There are several fields missing when the PKG method is called from ServiceLayer ONLY when called from within that particular test method. b) Using API 27 on the calling classes has not made a difference. Btw I just updated my question to include the PKG version for the 2 calling classes (they're on the same version). – Mossi Jul 1 '15 at 20:34
  • What i understand is that all the three classes(TestClass, ServiceLayer, UtilityClass) are inside same package "PKG version 1.55", Right? – Vikas Khandelwal Jul 2 '15 at 2:31
  • Not right. Only UtilityClass is inside a package. – Mossi Jul 2 '15 at 7:49

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.