运行 批量顶点测试方法作为标准用户
Run batch apex test method as standard user
感谢阅读。
我正在尝试编写一个测试方法,该方法 运行 是一个批处理顶点,用于使用没有删除权限的标准用户删除案例,但每当我 运行 测试时,我都会看到我当前(管理员)用户下的批次 运行s。我错过了什么吗?
这是我的批处理代码:
global with sharing class BatchDelete implements
Database.Batchable<sObject>, Database.Stateful {
// instance member to retain state across transactions
global Integer recordsProcessed = 0;
global Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator(
'SELECT ID ' +
'FROM Case '
);
}
global void execute(Database.BatchableContext bc, List<Case> scope){
System.debug('---------BATCH----------');
System.debug('Current BATCH User: ' + UserInfo.getUserName());
System.debug('isAccessible: ' + Case.sObjectType.getDescribe().isDeletable());
System.debug('------------------------');
// process each batch of records
List<Case> cases = new List<Case>();
for (Case c1 : scope) {
cases.add(c1);
}
delete cases;
}
global void finish(Database.BatchableContext bc){
System.debug(recordsProcessed + ' records processed. Shazam!');
} }
还有我的测试方法代码:
@isTest(SeeAllData=false)
static void test_readOnlyUser() {
UserLicense uL =[SELECT Id,LicenseDefinitionKey,Name FROM UserLicense WHERE Name = 'Salesforce'];
Profile p = [SELECT Id FROM Profile WHERE UserLicenseId =: uL.Id AND UserType = 'Standard'
AND PermissionsModifyAllData = false AND PermissionsViewAllData = false limit 1 ];
User u = new User(
UserName = 'test-user@test-company.com',
FirstName = 'Test-First-Name',
LastName = 'Test-Last-Name',
Alias = 'test',
Email = 'test-user@test-company.com',
EmailEncodingKey = 'UTF-8',
LanguageLocaleKey = 'en_US',
LocalesIdKey = 'en_US',
TimezonesIdKey = 'America/Los_Angeles',
ProfileId = p.Id
);
Case c1 = new Case();
c1.Status = 'New';
insert c1;
Test.startTest();
System.runAs(u) {
System.debug('---------TEST-----------');
System.debug('Current User: ' + UserInfo.getUserName());
System.debug('isAccessible: ' + Case.sObjectType.getDescribe().isDeletable());
System.debug('------------------------');
BatchDelete myBatchObject = new BatchDelete();
Id batchId = Database.executeBatch(myBatchObject);
}
Test.stopTest();
} }
您需要将 Test.startTest()
和 Test.stopTest()
移动到 runAs
调用中。这是因为批处理是异步的,所以只能保证在Test.stopTest()
被调用的那一刻已经执行。
System.runAs(u) {
Test.startTest();
//your batch code
Test.stopTest();
}
感谢阅读。
我正在尝试编写一个测试方法,该方法 运行 是一个批处理顶点,用于使用没有删除权限的标准用户删除案例,但每当我 运行 测试时,我都会看到我当前(管理员)用户下的批次 运行s。我错过了什么吗?
这是我的批处理代码:
global with sharing class BatchDelete implements
Database.Batchable<sObject>, Database.Stateful {
// instance member to retain state across transactions
global Integer recordsProcessed = 0;
global Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator(
'SELECT ID ' +
'FROM Case '
);
}
global void execute(Database.BatchableContext bc, List<Case> scope){
System.debug('---------BATCH----------');
System.debug('Current BATCH User: ' + UserInfo.getUserName());
System.debug('isAccessible: ' + Case.sObjectType.getDescribe().isDeletable());
System.debug('------------------------');
// process each batch of records
List<Case> cases = new List<Case>();
for (Case c1 : scope) {
cases.add(c1);
}
delete cases;
}
global void finish(Database.BatchableContext bc){
System.debug(recordsProcessed + ' records processed. Shazam!');
} }
还有我的测试方法代码:
@isTest(SeeAllData=false)
static void test_readOnlyUser() {
UserLicense uL =[SELECT Id,LicenseDefinitionKey,Name FROM UserLicense WHERE Name = 'Salesforce'];
Profile p = [SELECT Id FROM Profile WHERE UserLicenseId =: uL.Id AND UserType = 'Standard'
AND PermissionsModifyAllData = false AND PermissionsViewAllData = false limit 1 ];
User u = new User(
UserName = 'test-user@test-company.com',
FirstName = 'Test-First-Name',
LastName = 'Test-Last-Name',
Alias = 'test',
Email = 'test-user@test-company.com',
EmailEncodingKey = 'UTF-8',
LanguageLocaleKey = 'en_US',
LocalesIdKey = 'en_US',
TimezonesIdKey = 'America/Los_Angeles',
ProfileId = p.Id
);
Case c1 = new Case();
c1.Status = 'New';
insert c1;
Test.startTest();
System.runAs(u) {
System.debug('---------TEST-----------');
System.debug('Current User: ' + UserInfo.getUserName());
System.debug('isAccessible: ' + Case.sObjectType.getDescribe().isDeletable());
System.debug('------------------------');
BatchDelete myBatchObject = new BatchDelete();
Id batchId = Database.executeBatch(myBatchObject);
}
Test.stopTest();
} }
您需要将 Test.startTest()
和 Test.stopTest()
移动到 runAs
调用中。这是因为批处理是异步的,所以只能保证在Test.stopTest()
被调用的那一刻已经执行。
System.runAs(u) {
Test.startTest();
//your batch code
Test.stopTest();
}