1

I have custom objects with some standard fields and some custom fields and I want to be able to load test data using Test.loadData() and static resources.

The trouble is that I can't hard-code the namespace in the static resources because every developer has different namespace in their orgs. The call to Test.loadData() errors out because it can't the field myNamespace__myField__c on an org that had a different namespace.

So my question is, can I load test data from static resources without hard-coding the namespace in the resource? Or, would it be less overhead to just instruct the other developers to replace the namespace in the static resources before running the tests?

3
  • 1
    We solve namespace issues as a build step. We use a token to represent the namespace, and the ant job we use to deploy codes to the org replaces that token with the valid namespace for the org we use. Each developer could have their own override settings in ant to set their own namespaces. If you're interested I could share a bit more of this as an answer.
    – mkorman
    Dec 18, 2015 at 14:59
  • Hello @mkorman and thanks for the reply. Yes, I'd be interested in more detail. Does the ant script pre-pend the namespace to the values from certain static resource files? If so, is there some intelligence built in that distinguishes when to include the namespace and when not to? Dec 18, 2015 at 18:17
  • We add a token to our source code that gets replaced by the namespace on build. I will post some examples if I have more time.
    – mkorman
    Dec 19, 2015 at 9:15

1 Answer 1

1

This is how we've solved this. Essentially, we use an ant build step to replace a token by the right namespace.

Our actual source code (what we check into SVN) may have this line:

private @@namespace@@__CustomObject__c customObj = null;
private @@namespace@@.GlobalClass instance = null;

...

if (customObj.@@namespace@@__CustomField__c == null) ...

Every time we reference an object, field or class from our package, we use the @@namespace@@ token. This would never compile, as it's not valid language syntax.

Ant to the rescue! We use the Force.com migration tool, which is based on ant. We have added an ant target that looks like this:

<target name="prepareDeployment"
    description="Prepares the files for deployment"
    depends="cleanUpDeployment">
    <echo message="Preparing deployment files..." />
    <copy todir="deploy">
        <fileset dir="..\src"/>
    </copy>
    <echo message="Setting namespace..." />
    <replace dir="deploy" token="@@namespace@@" value="${user.namespace}"/>
</target>

This target essentially copies all files in the "src" folder to the "deploy" directory, then runs the token replacement.

user.namespace is a variable defined in the user-specific settings. This way each developer could have his own namespace.

This target would be a dependency for the actual deployment ant target.

Every time we deploy our code to our org, it runs the "prepare" target, replaces the namespace token with the actual namespace, and the code will compile and pass tests in the SF org.

2
  • Great solution! Thanks for posting such a detailed account of it. Dec 21, 2015 at 13:12
  • Glad it could help. I have edited a minor issue in my post. You obviously cannot reference to public classes from your package, but to global ones instead
    – mkorman
    Dec 21, 2015 at 16:08

You must log in to answer this question.

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