[SalesForce] Passing parameters while testing REST web service

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.

Best Answer

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();

}
Related Topic