简体   繁体   English

带有REST服务的Salesforce APEX单元测试错误/案例对象选择时发生错误

[英]Salesforce APEX Unit Test Error with REST Services / Error at SELECT of Case Object

I've succeeded to successfully construct a REST API using APEX language defined with an annotation: @RestResource. 我已成功使用带有注释的@@ RestResource定义的APEX语言成功构建了REST API。

I also wrote a matching Unit test procedure with @isTest annotation. 我还编写了一个带有@isTest批注的匹配的单元测试过程。 The execution of the REST API triggered by a HTTP GET with two input parameters works well, while the Unit Test execution, returns a "null" value list resulting from the SOQL query shown below: 由具有两个输入参数的HTTP GET触发的REST API的执行效果很好,而执行单元测试时,返回如下所示的SOQL查询产生的“空”值列表:

String mycase = inputs_case_number; // for ex. '00001026'   
sObject[] sl2 = [SELECT Id, CaseNumber FROM Case WHERE CaseNumber = :mycase LIMIT 1];

The query returns: 查询返回:

VARIABLE_ASSIGNMENT [22]|sl2|[]|0x1ffefea6

I've also tried to execute it with a RunAs() method (see code below), using a dynamically created Salesforce test user, not anonymous, connected to a more powerful profile, but still receiving a "null" answer at the SOQL query. 我还尝试使用RunAs()方法(请参见下面的代码)执行该操作,方法是使用动态创建的Salesforce测试用户(不是匿名用户)连接到功能更强大的配置文件,但仍会在SOQL查询中收到“空”答案。 The new profile defines "View All" permission for Cases. 新的配置文件为案例定义了“查看全部”权限。 Other SOQL queries to objects like: "User" and "UserRecordAccess" with very similar construction are working fine, both for REST APEX and Test APEX. 对于诸如REST APEX和Test APEX的结构非常相似的对象,诸如“ User”和“ UserRecordAccess”之类的其他SOQL查询也可以正常工作。

Is there a way to configure an access permission for Unit test (@isTest) to read the Case object and a few fields like: Id and CaseNumber. 有没有一种方法可以配置对单元测试(@isTest)的访问权限,以读取Case对象和一些字段,例如:Id和CaseNumber。 Is this error related to the "Tooling API" function and how can we fix this issue in the test procedure? 此错误与“工具API”功能有关,我们如何在测试过程中解决此问题?

Code attachment: Unit Test Code 代码附件: 单元测试代码

@isTest
private class MyRestResource1Test {
     static testMethod void MyRestRequest() { 
     // generate temporary test user object and assign to running process
     String uniqueUserName = 'standarduser' + DateTime.now().getTime() + '@testorg.com';
     Profile p = [SELECT Id FROM Profile WHERE Name='StandardTestUser'];
     User pu = new User(Alias='standt',Email='standarduser@testorg.com',LastName='testing',EmailEncodingKey='UTF-8',LanguageLocaleKey='en_US',LocaleSidKey='en_US',ProfileId=p.Id,TimeZoneSidKey='America/New_York',UserName=uniqueUserName);
     System.RunAs(pu) {
          RestRequest req = new RestRequest();
          RestResponse res = new RestResponse(); 
          req.requestURI = '/services/apexrest/sfcheckap/';
          req.addParameter('useremail','testuserid@red.com');
          req.addParameter('casenumber','00001026'); 
          req.httpMethod = 'GET';
          RestContext.request = req;
          RestContext.response = res;
          System.debug('Current User assigned is: ' + UserInfo.getUserName());
          System.debug('Current Profile assigned is: ' + UserInfo.getProfileId()); 
          Test.startTest(); 
          Map<String, Boolean> resultMap = MyRestResource1.doGet(); 
          Test.stopTest();
          Boolean debugflag = resultMap.get('accessPermission');
          String debugflagstr = String.valueOf(debugflag);
          System.assert(debugflagstr.contains('true'));
        }   
    }  
}

Found a solution path by using: @isTest(SeeAllData=true) 通过使用以下方法找到解决方案路径: @isTest(SeeAllData = true)

See article: "Using the isTest(SeeAllData=true) Annotation" 请参阅文章:“使用isTest(SeeAllData = true)注释”

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM