+ Start a Discussion
Sourav Pradhan 3Sourav Pradhan 3 

I am getting 86% code coverage and not 100%. Please help asap.

thirdParty.apxc
============
@RestResource(urlMapping='/v1/GetAccount/')
 global with sharing class thirdParty {
     @HttpGet
     global static Case__c getCaseById() {
          Case__c acc=new Case__c();
      Map <String,String> mapparam= RestContext.request.params;
        String dataId=mapparam.get('Id');
        acc=[SELECT Date__c,Case_Origin__c,Case_Reason__c,Case_Status__c,Name,Products__c,Subject__c FROM Case__c WHERE Id = :dataId];
         return acc;
     }
     @HttpPost
    global static Case__c addAccount(String origin,String reason,String status,String product,String subject,string cus){
        Case__c acc=new Case__c(Case_Origin__c=origin, 
                             Case_Reason__c=reason,
                             Case_Status__c=status,
                             Products__c=product,
                             Subject__c=subject,
                             Customer_Name__c= cus );
        insert acc;
        return acc;
    }
}

thirdPartyTest.apxc
==================
@istest
public class thirdPartyTest{
 @isTest 
   static void getCaseById() {
        //Id recordId = createTestRecord();
        RestRequest request = new RestRequest();
        request.requestUri =
            'https://yourInstance.salesforce.com/services/apexrest/Cases/'
            ;
        request.httpMethod = 'GET';
        RestContext.request = request;
        
        Case__c thisCase = thirdParty.getCaseById();
        
        System.assert(thisCase != null);
        System.assertEquals('Test record', thisCase.Subject__c);
        
    }
       @isTest 
   static void addAccount() {
        
        thirdParty.addAccount(
            'Web', 'Damage', 'Approved', 'Yamaha Fascino 125','Damage Issue','a005g00002t5vqdAAA');
       
        //System.assert(thisCaseId != null);
        Case__c thisCase = [SELECT Id,Subject__c FROM Case__c ];
        System.assert(thisCase != null);
        System.assertEquals(thisCase.Subject__c, 'Damage Issue');
    }
}User-added image
Suraj Tripathi 47Suraj Tripathi 47
Hi sourav,

You can take reference from this below test class and 100% coverage:-
@istest
public class thirdPartyTest{
    @isTest 
    static void getCaseById() {
        //Id recordId = createTestRecord();
        Case__c ca= new Case__c();
        ca.Name='test';
        ca.Date__c=date.today();
        ca.Subject__c='Test record';
        insert ca;
        id caseId=ca.id;
        
        RestRequest request = new RestRequest();
        request.requestUri ='https://yourInstance.salesforce.com/services/apexrest/Cases/'
            ;
        request.params.put('Id',caseId);
        request.httpMethod = 'GET';
        RestContext.request = request;
        
        Case__c thisCase = thirdParty.getCaseById();
        
        System.assert(thisCase != null);
        System.assertEquals('Test record', thisCase.Subject__c);
    }
    @isTest 
    static void addAccount() {
        
        thirdParty.addAccount(
            'Web', 'Damage', 'Approved', 'Yamaha Fascino 125','Damage Issue','a005g00002t5vqdAAA');
        
        //System.assert(thisCaseId != null);
        Case__c thisCase = [SELECT Id,Subject__c FROM Case__c ];
        System.assert(thisCase != null);
        System.assertEquals(thisCase.Subject__c, 'Damage Issue');
    }
}

In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 

Thanks and Regards
Suraj Tripathi.