4

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?

2 Answers 2

3

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.

2

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.

You must log in to answer this question.

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