Has anyone wanted to provide a 'Test tool' with a managed package? Something like:

@IsTest global class SomeTestHelper {
    global String findSomething() {
        return '...';

that you could expose for consumption by other namespaces only in unit test context?

Is this possible?

up vote 3 down vote accepted

Yes, it is. I've updated a previously global class with the @isTest identifier and it works almost as you'd expect.

However referencing the method from non-test classes is allowed by the compiler, but throws a System.TypeException at runtime if the code path actually tries to execute the testclass-contained method.

I have similar global static methods within a global class that is annotated with @IsTest currently deployed in a managed package. They can be used by external applications for setting up test Opportunities. This makes it easier for installers to test classes that implement an interface that is also defined in the managed package.

If you want to ensure that the methods are only called from a unit test context you could start when with

System.assert(Test.isRunningTest(), 'XYZ() method can only be used in a testing context');

It won't help at compile time or during testing, but it will ensure the methods aren't used outside of tests.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.