APEX,单元测试,标注没有静态资源的响应

时间:2013-09-13 21:13:37

标签: json unit-testing salesforce apex-code

有点困在另一个我害怕,我正在尝试为批量APEX课程编写单元测试。

该类对google api有一个calllout,所以我创建了一个静态资源,我通过模拟进入,所以我可以完成处理返回的JSON的测试。但由于某种原因,响应始终为空。

现在非常奇怪的是,如果我使用完全相同的callout / JSON代码,并且在之前的@future调用中使用相同的模拟代码,那么它确实返回正常。

这是班级:

global class mileage_bulk implements Database.Batchable<sObject>,
  Database.AllowsCallouts
{
global Database.QueryLocator start(Database.BatchableContext BC)
{
String query = 'SELECT Id,Name,Amount,R2_Job_Ref__c,R2_Shipping_Post_Code__c,Shipping_Postcode_2__c FROM Opportunity WHERE R2_Shipping_Post_Code__c != null';
return Database.getQueryLocator(query);
//system.debug('Executing'+query);
}

global void execute(Database.BatchableContext BC, List<Opportunity> scope)
{
system.debug(scope);
for(Opportunity a : scope)
{

    String startPostcode = null;
    startPostcode = EncodingUtil.urlEncode('HP27DU', 'UTF-8');
    String endPostcode = null;
    String endPostcodeEncoded = null;
    if (a.R2_Shipping_Post_Code__c != null){
    endPostcode =   a.R2_Shipping_Post_Code__c;
    Pattern nonWordChar = Pattern.compile('[^\\w]');
    endPostcode = nonWordChar.matcher(endPostcode).replaceAll('');
    endPostcodeEncoded =   EncodingUtil.urlEncode(endPostcode, 'UTF-8');
        }   
    Double totalDistanceMeter = null;
    Integer totalDistanceMile = null;
   String responseBody = null;
   Boolean firstRecord = false;

    String ukPrefix = 'UKH';
    if (a.R2_Job_Ref__c != null){    
    if ((a.R2_Job_Ref__c).toLowerCase().contains(ukPrefix.toLowerCase())){
    system.debug('Is Hemel Job');
    startPostcode = EncodingUtil.urlEncode('HP27DU', 'UTF-8');
    } else {
    system.debug('Is Bromsgrove Job');
    startPostcode = EncodingUtil.urlEncode('B604AD', 'UTF-8');
    }
    }

    // build callout
    Http h = new Http();
    HttpRequest req = new HttpRequest();
    req.setEndpoint('http://maps.googleapis.com/maps/api/directions/json?origin='+startPostcode+'&destination='+endPostcodeEncoded+'&units=imperial&sensor=false');
    req.setMethod('GET');
    req.setTimeout(60000);
    system.debug('request follows');
    system.debug(req);

try{  
        // callout
        HttpResponse res = h.send(req);

        // parse coordinates from response

        JSONParser parser = JSON.createParser(res.getBody());

        responseBody = res.getBody();
        system.debug(responseBody);

        while (parser.nextToken() != null) {
            if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && 
                (parser.getText() == 'distance')){
                   parser.nextToken(); // object start
                   while (parser.nextToken() != JSONToken.END_OBJECT){
                       String txt = parser.getText();
                       parser.nextToken();
                       //system.debug(parser.nextToken());
                       //system.debug(txt);
                       if (firstRecord == false){
                       //if (txt == 'text'){
                           //totalDistanceMile = parser.getText();
                           system.debug(parser.getText());
                       //}
                       if (txt == 'value'){
                           totalDistanceMeter = parser.getDoubleValue();
                           double inches = totalDistanceMeter*39.3701;
                           totalDistanceMile = (integer)inches/63360;
                           system.debug(parser.getText());
                           firstRecord = true;
                       }
                       }
                   }

            }
        }


    } catch (Exception e) {
    }

//system.debug(accountId);
    system.debug(a);
    system.debug(endPostcodeEncoded);
    system.debug(totalDistanceMeter);
    system.debug(totalDistanceMile);

        // update coordinates if we get back 
        if (totalDistanceMile != null){
        system.debug('Entering Function to Update Object');
            a.DistanceM__c = totalDistanceMile;
            a.Shipping_Postcode_2__c = a.R2_Shipping_Post_Code__c;
            //update a;        
        }  
}
update scope;
}


global void finish(Database.BatchableContext BC)
{
}
}

这是测试类;

@isTest
private class mileage_bulk_tests{

static testMethod void myUnitTest() {
     Opportunity opp1 = new Opportunity(name = 'Google Test Opportunity',R2_Job_Ref__c = 'UKH12345',R2_Shipping_Post_Code__c = 'AL35QW',StageName = 'qualified',CloseDate = Date.today());
 insert opp1;
 Opportunity opp2 = new Opportunity(name = 'Google Test Opportunity 2',StageName = 'qualified',CloseDate = Date.today());
 insert opp2;
 Opportunity opp3 = new Opportunity(name = 'Google Test Opportunity 3',R2_Job_Ref__c = 'UKB56789',R2_Shipping_Post_Code__c = 'AL35QW',StageName = 'qualified',CloseDate = Date.today());
insert opp3;


StaticResourceCalloutMock mock = new StaticResourceCalloutMock();
mock.setStaticResource('googleMapsJSON');
mock.setStatusCode(200); // Or other appropriate HTTP status code
mock.setHeader('Content-Type', 'application/json'); // Or other appropriate MIME type like application/xml

//Set the mock callout mode
Test.setMock(HttpCalloutMock.class, mock);

system.debug(opp1);
system.debug(opp1.id);

//Call the method that performs the callout
Test.startTest();
mileage_bulk b = new mileage_bulk();
database.executeBatch((b), 10);
Test.stopTest();
    }
}

非常感谢!

由于

加雷

1 个答案:

答案 0 :(得分:2)

  1. 不确定'googleMapsJSON'的样子,也许你可以为我们发帖。
  2. 假设您的模拟资源格式正确,请确保文件扩展名为“.json”,并使用UTF-8编码保存。

    如果#2不起作用,你应该尝试将你的资源保存为.txt - 在它需要纯文本资源但期望应用程序/ json内容类型 <之前我已经运行过了/ p>

  3. 确保您提供的资源名称字符串与资源名称具有相同的大小写。它区分大小写。

  4. 您是在开发命名空间的包环境吗?如果是,请尝试将名称空间添加到资源名称。
  5. 否则,乍一看你的代码看起来很不错。