Pergunta

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 that 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!!

Foi útil?

Solução

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top