+ Start a Discussion
Nihon TaisaiNihon Taisai 

How to make Test Coverage 100%? HttpGet method


How can I test this for 100% coverage?
@HttpGet
global static List<Box__c> getBox(){
    RestRequest req = RestContext.request;
    String boxId = req.params.get('Id');
    if(boxId == null)
    {
        return [SELECT Id, Title__c
                FROM Box__c
                LIMIT 1000];
    }
    else
    {
        List<Box__c> boxList;
        boxList = [SELECT Id, Title__c 
                   FROM Box__c
                   WHERE Id =: boxId
                   LIMIT 1000];
        return boxList;
    }
}
My test It works, but only 80%:
@isTest
static void testBox() {
    Id recordId = idd();
    RestRequest request = new RestRequest();
    request.requestUri = System.URL.getSalesforceBaseUrl().toExternalForm() + '/services/apexrest/Box__c/' + recordId;            
    request.httpMethod = 'GET';
    RestContext.request = request;

    List<Box__c> b = OurClass.getBox();

    for(Box__c pneBox : b){
        System.assertEquals(pneBox.Id, recordId);
    }
}

static Id idd() {
    Box__c bb = new Box__c(
        Title__c = 'Title',
    );
    insert bb;
    return bb.Id;
}
Best Answer chosen by Nihon Taisai
Amit Chaudhary 8Amit Chaudhary 8
Try below code
 
@isTest
static void testBox() {
    Id recordId = idd();
    RestRequest request = new RestRequest();
    request.requestUri = System.URL.getSalesforceBaseUrl().toExternalForm() + '/services/apexrest/Box__c/' + recordId;            
    request.httpMethod = 'GET';
	request.addParameter('Id', recordId);
	RestContext.request = request;

    List<Box__c> b = OurClass.getBox();
    for(Box__c pneBox : b){
        System.assertEquals(pneBox.Id, recordId);
    }
}

@isTest
static void testBoxWithoutID(){
    RestRequest request = new RestRequest();
    request.requestUri = System.URL.getSalesforceBaseUrl().toExternalForm() + '/services/apexrest/Box__c/';            
    request.httpMethod = 'GET';
    RestContext.request = request;
    List<Box__c> b = OurClass.getBox();
    for(Box__c pneBox : b){
        System.assertEquals(pneBox.Id, recordId);
    }
}

static Id idd() {
    Box__c bb = new Box__c(
        Title__c = 'Title',
    );
    insert bb;
    return bb.Id;
}

Let us know if this will help you
 

All Answers

Raj VakatiRaj Vakati
Try like below
 
@isTest
static void testBox() {
    Id recordId = idd();
    RestRequest request = new RestRequest();
	  RestResponse res = new RestResponse();
    request.requestUri = System.URL.getSalesforceBaseUrl().toExternalForm() + '/services/apexrest/Box__c/' + recordId;   
        request.addParameter('Id', recordId);
	
    request.httpMethod = 'GET';
    RestContext.request = request;
  RestContext.response = res;
	 

		
		
    List<Box__c> b = OurClass.getBox();

    for(Box__c pneBox : b){
        System.assertEquals(pneBox.Id, recordId);
    }
}

static Id idd() {
    Box__c bb = new Box__c(
        Title__c = 'Title',
    );
    insert bb;
    return bb.Id;
}

 
Amit Chaudhary 8Amit Chaudhary 8
Try below code
 
@isTest
static void testBox() {
    Id recordId = idd();
    RestRequest request = new RestRequest();
    request.requestUri = System.URL.getSalesforceBaseUrl().toExternalForm() + '/services/apexrest/Box__c/' + recordId;            
    request.httpMethod = 'GET';
	request.addParameter('Id', recordId);
	RestContext.request = request;

    List<Box__c> b = OurClass.getBox();
    for(Box__c pneBox : b){
        System.assertEquals(pneBox.Id, recordId);
    }
}

@isTest
static void testBoxWithoutID(){
    RestRequest request = new RestRequest();
    request.requestUri = System.URL.getSalesforceBaseUrl().toExternalForm() + '/services/apexrest/Box__c/';            
    request.httpMethod = 'GET';
    RestContext.request = request;
    List<Box__c> b = OurClass.getBox();
    for(Box__c pneBox : b){
        System.assertEquals(pneBox.Id, recordId);
    }
}

static Id idd() {
    Box__c bb = new Box__c(
        Title__c = 'Title',
    );
    insert bb;
    return bb.Id;
}

Let us know if this will help you
 
This was selected as the best answer