3

I'm trying to test a REST service I created, but my second parameter is coming back null in my URI.

Here is my class:

@RestResource(urlMapping='/ConvertLeadFromApp/*')
global with sharing class RESTLeadConvertController {

@HttpGet
global static Lead getConvertedLead() {
    Lead result;
    try {
        String leadId = RestContext.request.params.get('leadId'); 
        Boolean isQueueOwner = Boolean.valueOf(RestContext.request.params.get('isQueueOwner'));     
        Database.LeadConvert lc = new Database.LeadConvert(); 
        lc.setLeadId(leadId);
        if(isQueueOwner) {
            User appUser = [select Id from User where name = 'App' limit 1];
            lc.setOwnerId(appUser.Id);
        }
        lc.setDoNotCreateOpportunity(true);
        LeadStatus convertstatus = [select Id, MasterLabel from LeadStatus where IsConverted = true limit 1];
        lc.setConvertedStatus(convertStatus.MasterLabel); 
        Database.LeadConvert[] lcArray = new Database.LeadConvert[] {lc}; 
        Database.LeadConvertResult[] results = Database.convertLead(lcArray);   
        result = [SELECT Id, email, status, isConverted, convertedAccountId, convertedContactId FROM Lead WHERE Id = :leadId];
    }
    catch(System.StringException e){
        RestContext.response.statusCode = 400;
        RestContext.response.responseBody = Blob.valueOf('Param: leadId is not a valid Id');
    }
    catch(System.QueryException e){
        RestContext.response.statusCode = 404;
        RestContext.response.responseBody = Blob.valueOf('Requested Lead not found');
    }
    catch(Exception e){ //something else happened
        RestContext.response.statusCode = 500;
        RestContext.response.responseBody = Blob.valueOf(e.getMessage());
    }
    return result;
}   
}

Here is my test class:

@isTest
private class RESTLeadConvertControllerTest {

static Lead testLead;

static void initialize() {
    testLead = TestFactory.createTestLead('John', 'Doe', 'Acme', 'Open', true);
    system.assertNotEquals(null, testLead.Id);
}


static testMethod void testRESTLeadConvert() {
    initialize();

    RestRequest req = new RestRequest(); 
    RestResponse res = new RestResponse();
    req.requestURI = '/services/apexrest/ConvertLeadFromApp?leadId=' + testLead.Id + '&isQueueOwner=' + false + '';  
    req.httpMethod = 'GET';
    RestContext.request = req;
    RestContext.response = res;

    Lead result = RESTLeadConvertController.getConvertedLead();

}
}

My test fails on this line as a null argument: Boolean isQueueOwner = Boolean.valueOf(RestContext.request.params.get('isQueueOwner'));

Am I not setting the argument in the unit test correctly? I ran my service in workbench and it is working.

  • Try passing in 'false' as a string, rather than a Boolean... like this req.requestURI = '/services/apexrest/ConvertLeadFromApp?leadId=' + testLead.Id + '&isQueueOwner=false'; It looks like your RestResource is expecting a string because you are doing the type conversion to Boolean (second line in the try block) – John Thompson Feb 22 '17 at 20:31
  • @JohnThompson I tired that as well and I'm still getting a null for that parameter. – Dman100 Feb 22 '17 at 21:16
  • 1
    @JohnThompson I figured it out. I needed to add the parameters to the request object req.addParameter('leadId', testLead.Id); req.addParameter('isQueueOwner', 'false'); That fixed it! Thanks for your help! – Dman100 Feb 22 '17 at 22:13
2

Since the test isn't actually making a REST request to that URI (it's just setting a global context var), we can't actually unit test our urlMapping parameter. Instead, we have to add the parameters to the RestRequest object.

static testMethod void testRESTLeadConvert() {
    initialize();

    RestRequest req = new RestRequest(); 
    RestResponse res = new RestResponse();
    req.requestURI = '/services/apexrest/ConvertLeadFromApp/';  
    req.addParameter('leadId', testLead.Id);
    req.addParameter('isQueueOwner', 'false');
    req.httpMethod = 'GET';
    RestContext.request = req;
    RestContext.response = res;

    Lead result = RESTLeadConvertController.getConvertedLead();

}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.