2011-03-16 99 views
1

我在SalesForce Apex代码中看到一些行为,我不明白。它似乎破坏了代码安全规则。我有一个控制器,它看起来是这样的:Apex访问说明符和测试类

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 
    } 
} 

和测试,看起来是这样的:

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 

    } 
} 

请注意,我的控制器有“TheCase”私人二传手,但我能在测试课上改变它的价值。此代码可以工作并由SalesForce处理。为什么?对于允许他们访问其他类的私有成员的测试类,有什么特别的吗?

谢谢!

回答

1

杜,早就没有想清楚。设置者对案例属性本身是私有的,但案例的属性(如主题)仍然可公开访问。

使案例属性专用设置仅保护控制器不会从案例下方更改案例。

对不起!

+0

补充一下 - 我看到在我的RSS源,并认为我会得到一个简单的10分;-) – metadaddy 2011-03-17 03:24:59