4

I am writing a test class that needs to be instance-agnostic. On some of our instances we have a managed package that affects the outcome of the test, so I need to insert some test data that will be used by that managed package, but I'm worried that if I migrate this test case to instances without that managed package, it will fail when trying to insert this test data for objects that don't exist on that instance.

To make a long question shorter, is there a way to test for the presence or absence of either a specific object or a managed package so I can run this code conditionally?

5

Your best approach is probably to use a combination of testing for the presence of the package, and then checking the running user has access in case there are licence restrictions in place.

@user320 answer offers some good suggestions on how to detect a package - if there are record types, they are also a good option in my experience.

If the package exists, I would check that the running user has access to the package with this method on userinfo so that you know they can write to fields on the package:

userinfo.isCurrentUserLicensed('kw');

Note - if the namespace does NOT exist, you'll get an exception, so I would so something like this just in case:

Boolean bPackage = FALSE;
try{
    bPackage = userinfo.isCurrentUserLicensed('kw');
}
Catch (Exception Ex) {
system.debug(ex);
}
if (bPackage) {
//Insert test data
};

Also, you'll need to make sure your data inserts don't explicitly reference any fields in the package directly, or you will create a dependency, but if you use dynamic DML with put statements, you should be fine.

|improve this answer|||||
4

While Userinfo.isCurrentUserLicensed() is a good first port of call, I'd humbly recommend detecting the presence of a global Apex class or SObject that you know for sure will be in the package:

if (Type.forName('namespace.ClassWithGlobalVisibility') != null) {
    //your test data here
}

or

if (Type.forName('namespace__SalesforceObject__c') != null) {
    //your test data here
}

This is license-agnostic; it works even if your customers have the package installed but no license.

|improve this answer|||||
  • 1
    That should work - my only hesitation with recommending that approach was that with user-licensed packages, you might need a licence in order to be able to write to the fields - so depending on the package you are trying to detect, a combination of the two might be prudent! – BritishBoyinDC Feb 24 '14 at 21:49
  • 1
    Nice observation! Let's join forces, and add that to your answer? @BritishBoyinDC – bigassforce Feb 24 '14 at 21:59
  • 1
    I'm relatively new to StackExchange. I’ve marked @BritishBoyinDC’s answer as the correct one, but is there any way for me to give @user320 some credit for this answer, too? – Joey Day Mar 13 '14 at 18:26

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.