Salesforce Apex:如何正确实施 Trailhead 示例中的简单单元测试?

问题描述 投票:0回答:1

我正在遵循 Apex Rest Callouts Trail,它指导您为示例类编写单元测试。

所有代码都已给出,您只需将其添加到他们的ide中即可。

问题是,一行代码错误,而且我事先没有 Apex 知识,所以我有点迷失了。

有问题的班级是:

@isTest
private class AnimalsCalloutsTest {
    @isTest static  void testGetCallout() {
        // Create the mock response based on a static resource
        StaticResourceCalloutMock mock = new StaticResourceCalloutMock();
        mock.setStaticResource('GetAnimalResource');
        mock.setStatusCode(200);
        mock.setHeader('Content-Type', 'application/json;charset=UTF-8');
        // Associate the callout with a mock response
        Test.setMock(HttpCalloutMock.class, mock);
        // Call method to test
        HttpResponse result = AnimalsCallouts.makeGetCallout();
        // Verify mock response is not null
        System.assertNotEquals(null,result, 'The callout returned a null response.');
        // Verify status code
        System.assertEquals(200,result.getStatusCode(), 'The status code is not 200.');
        // Verify content type   
        System.assertEquals('application/json;charset=UTF-8',
          result.getHeader('Content-Type'),
          'The content type value is not expected.');  
        // Verify the array contains 3 items     
        Map<String, Object> results = (Map<String, Object>) 
            JSON.deserializeUntyped(result.getBody());
        List<Object> animals = (List<Object>) results.get('animals');
        System.assertEquals(3, animals.size(), 'The array should only contain 3 items.');          
    }   
}

错误位于第 10 行,并显示

Method does not exist or incorrect signature: void setMock(System.Type, System.StaticResourceCalloutMock) from the type Test

IDE 非常简陋,因此很难看出每个表达式的类型,但似乎签名不正确(该方法存在)。

我不确定它期望什么,并且进一步跟踪会导致稍后出现相同的错误。

salesforce apex trailblazer
1个回答
0
投票

我会检查其中一些事情:

命名空间前缀, API版本, 依赖性问题。

© www.soinside.com 2019 - 2024. All rights reserved.