开发者

Apex Access Specifiers and Test Classes

I am seeing some behavior in the SalesForce Apex code that I don't understand. It seems to break code security rules. I have a controller that looks something like this:

public with sharing class CaseController {

    //Properties
    public Case TheCase { get; private set; }

    //
    //Constructor
    public CaseController(ApexPages.StandardController controller) {
        //Some unimportant stuff
    }

    //
    //Validates all data coming in from the view and saves the case
    public PageReference Save() {
        //Some other unimportant stuff
    }
 }

And a test that looks something like this:

private static testMethod void Save_WithCompleteCase_SavesCase() 
{
    //Given
    User user = GetTestUser('Standard User');
    Product2 theProduct = GetTestProduct();
    Case theCase = GetTestCase(user, theProduct);

    System.runAs(user) {

        //When
        CaseController controller = new CaseController(new ApexPages.StandardController(theCase));
        controller.TheCase.Subject = 'Test Case'; //Making a change to test it saved
        PageReference page = controller.Save();

        //Then

    }
 }

Note th开发者_如何学编程at my controller has a private setter on "TheCase", yet I am able to change its value in the test class. This code works and is processed by SalesForce. Why? Is there something special about test classes that allow them to access private members of other classes?

Thanks!!


Duh, wasn't thinking clearly earlier. The setter is private on the Case Property itself, but the Properties of Case, such as subject, are still publicly accessible.

Making the Case property private set only protects the controller from having the Case change from underneath it.

Sorry!!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜