Invoke Private / Protected Method
Sometimes you will need to invoke a private / protected method for testing. Apex offers a simple invokeMethod() within the testing library to facilitate this. For example:
public function testInvoke(): void
{
$obj = new MyObject();
$res = $this->invokeMethod($obj, 'myProtectedMethod', ['abc', '123']);
$this->assertEquals($res, 'yes');
}
The above creates a new instance of MyObject()
, calls the private / protected method of myProtectedMethod()
, then returns the result.