salesforce 零基础学习(五十七)Test 类中创建TestUser帮助类

我们写Test Class的时候往往都需要指定一个uesr去run test method。

TestUserHelper类如下:

 1 public class TestUserHelper {
 2     public static User createUserByPermission(String permissionSetName) {
 3         Profile pfl = [select id from profile where name='System Administrator'];//the profile which you want to assign.
 4         //Declaring the User
 5         User runAsUser = new User(alias = 'Admin', email='admin@testorg.com',
 6                                   emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
 7                                   localesidkey='en_US', profileid = pfl.Id,  country='United States', CommunityNickname = 'u1',
 8                                   timezonesidkey='America/Los_Angeles', username='admin@xxx_org.com' );
 9         
10         Insert runAsUser;     
11         PermissionSet pSet = [SELECT ID,Label from PermissionSet WHERE Name=:permissionSetName];
12         PermissionSetAssignment Asign = New PermissionSetAssignment();
13         Asign.AssigneeId=runAsUser.ID;
14         Asign.PermissionSetId=pSet.ID;
15         Insert Asign;      
16         Return runAsUser; 
17     }
18     
19     public static User createUserByPermissionAndProfile(String permissionSetName,String profileName) {
20         Profile pfl = [select id from profile where name= :profileName];
21         //Declaring the User
22         User runAsUser = new User(alias = 'Admin', email='amin@testorg.com',
23                                   emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
24                                   localesidkey='en_US', profileid = pfl.Id,  country='United States', CommunityNickname = 'u1',
25                                   timezonesidkey='America/Los_Angeles', username='admin@xxx_org.com' );
26         
27         Insert runAsUser;     
28         PermissionSet pSet = [SELECT ID,Label from PermissionSet WHERE Name=:permissionSetName];
29         PermissionSetAssignment Asign = New PermissionSetAssignment();
30         Asign.AssigneeId=runAsUser.ID;
31         Asign.PermissionSetId=pSet.ID;
32         Insert Asign;      
33         Return runAsUser; 
34     }
35     
36 }

里面有两个静态方法,一个是创建一个Profile为Admin,指定的Permission Set的User,一个是指定Profile,指定Permission Set的User。相关Test Class引用:

 1 @isTest
 2 private class TestUserClassUsingUserHelper {
 3 
 4     static testMethod void myUnitTest() {
 5         User testUser = TestUserHelper.createUserByPermission('your test permisson set name');
 6         System.RunAs(testUser) {  
 7             Test.startTest();
 8             //TODO init data and operate like dml and assert etc
 9             Test.stopTest();
10         }
11     }
12 }

总结:此篇方便以后自己写TestClass创建TestUser,有需要的可以直接用,有错误的地方欢迎指出。

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏杨龙飞前端

scrollto 到指定位置

3435
来自专栏一个爱瞎折腾的程序猿

sqlserver使用存储过程跟踪SQL

USE [master] GO /****** Object: StoredProcedure [dbo].[sp_perfworkload_trace_s...

3700
来自专栏Ceph对象存储方案

Luminous版本PG 分布调优

Luminous版本开始新增的balancer模块在PG分布优化方面效果非常明显,操作也非常简便,强烈推荐各位在集群上线之前进行这一操作,能够极大的提升整个集群...

4415
来自专栏飞扬的花生

jsencrypt参数前端加密c#解密

      写程序时一般是通过form表单或者ajax方式将参数提交到服务器进行验证,如何防止提交的请求不被抓包后串改,虽然无法说绝对安全却给非法提交提高了难度...

4759
来自专栏张善友的专栏

Mix 10 上的asp.net mvc 2的相关Session

Beyond File | New Company: From Cheesy Sample to Social Platform Scott Hansel...

2997
来自专栏我和未来有约会

Silverlight第三方控件专题

这里我收集整理了目前网上silverlight第三方控件的专题,若果有所遗漏请告知我一下。 名称 简介 截图 telerik 商 RadC...

4885
来自专栏张善友的专栏

LINQ via C# 系列文章

LINQ via C# Recently I am giving a series of talk on LINQ. the name “LINQ via C...

3565
来自专栏一个会写诗的程序员的博客

Spring Reactor 项目核心库Reactor Core

Non-Blocking Reactive Streams Foundation for the JVM both implementing a Reactiv...

3502
来自专栏魂祭心

原 canvas绘制clock

5904
来自专栏我和未来有约会

Kit 3D 更新

Kit3D is a 3D graphics engine written for Microsoft Silverlight. Kit3D was inita...

3346

扫码关注云+社区