Is there a way, customer can create APEX object for testing? We are exposing APEX object for external developers to work on, but we do not expose constructor.( The object is built in the Java Layer and then get exposed to Apex. From Product point of view, we are not exposing constructing the object in Apex). Is there a concept of Test only constructor for Apex object?
1 Answer
You can use @TestVisible for that purpose:
public inherited sharing class MyClass {
@TestVisible
private MyClass() {
...
}
...
}
as that allows a test to use the constructor but non-test code cannot. From the linked documentation:
Use the TestVisible annotation to allow test methods to access private or protected members of another class outside the test class. These members include methods, member variables, and inner classes. This annotation enables a more permissive access level for running tests only. This annotation doesn’t change the visibility of members if accessed by non-test classes.