Salesforce Sharing and Visibility Designer Series Part 15

This entry is part 16 of 17 in the series Sharing and Visibility Designer Study Guide

Apex Unit Test for Sharing & Visibility 

To facilitate the development of robust, error-free code, Apex supports the creation and execution of unit tests. Unit tests are class methods that verify whether a particular piece of code is working properly.

Rules of writing unit test :

  • Use the isTest annotation to define classes and methods that only contain code used for testing your application.
  • The @isTest annotation on methods is equivalent to the testMethod keyword.
  • Classes and methods defined as isTest can be either private or public. The access level of test classes methods doesn’t matter.
  • The default access level in Apex is private. The testing framework can always find the test methods and execute them, regardless of their access level.
  • Classes defined as isTest must be top-level classes and can’t be interfaces or enums.
  • Methods of a test class can only be called from a running test, that is, a test method or code invoked by a test method, and can’t be called by a non-test request.

Positive case with single and multiple records

Test to verify that the expected behavior occurs through every expected permutation, that is, that the user filled out everything correctly and did not go past the limits.

Negative case with single and multiple records

There are likely limits to your applications, such as not being able to add a future date, not being able to specify a negative amount, and so on. You must test for the negative case and verify that the error messages are correctly produced as well as for the positive, within the limits cases.

Testing with other users

Test whether a user with restricted access to the sObjects used in your code sees the expected behavior. That is, whether they can run the code or receive error messages. Always use RunAs for to check user access on sObjects.

Salesforce help document shows example of each of these mechanism

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_example.htm

System Class Methods used for test classes

  • System.assert(): Asserts that the specified condition is true. If it is not, a fatal error is returned that causes code execution to halt.
  • System.assertEquals(): Asserts that the first two arguments are the same. If they are not, a fatal error is returned that causes code execution to halt
  • System.runAs(): Changes the current user to the specified user. (execute the test as a given user)

Salesforce help document shows example of each of these mechanism

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_example.htm

Questions

Q1. How do you enforce sharing in Apex code?

a.Configure Network settings

b.Using System.RunAs method

c.Groups sharing

d.Using “with sharing” in Apex classes.

Q2. Does an Inner class inherit sharing of outer class ?

a.TRUE

b.FALSE

Q3. Which of the following is an invalid statement when using DescribeSObjectResult class ?

a.IsCreatable

b.IsAccessible

c.IsDeletable

d.IsUpsertable

Q4. Which of the following statements is TRUE ?

a.Using runAs method in test methodenforces user permission on object ?

b.Using runAs method in test method enforces user field level permission ?

c.Use runAs to bypass mixed DML exception in test methods ?

d.runAs only enforce record sharing and ignore object and field permission ?

Answers

1.d

2.b

3.d

4.c,d

Series Navigation<< Salesforce Sharing and Visibility Designer Series Part 14Salesforce Sharing and Visibility Designer Series Part 16 >>
Posted in Contributors and tagged , .

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of