function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sahil YadavSahil Yadav 

Test Classes for Apex- Class

Hello Folks, I am trying to write the test class for one the apex class where I am getting the code coverage very less.
 
Apex Class :- 
@RestResource(urlMapping='/subscriptionUpdate/*')
global with sharing class subscriptionUpdate {    
    @HttpPost
    global static void doPost() {
        Map<String, Object> requestBody= (Map<String, Object>)JSON.deserializeUntyped(RestContext.request.requestBody.toString()); 
        Map<String, Object> requestBody1 = new Map<String, Object>();
        for(String s: requestBody.keySet()){
            requestBody1.put(s.toLowerCase(),requestBody.get(s));
        }
        system.debug('requestBody' +requestBody);
        system.debug('requestBody1===' +requestBody1);
        String oppId = String.valueOf(requestBody1.get('opportunityid'));
        String buyerId = String.valueOf(requestBody1.get('buyerid'));
        String subStatus = String.valueOf(requestBody1.get('subscriptionstatus'));
        String subsId = String.valueOf(requestBody1.get('subscriptionid'));
        if(oppId != null && oppId != ''){
            Opportunity opp = [SELECT ID, Name, Buy_ID__c, Subscription_Status__c FROM Opportunity WHERE Id = :oppId LIMIT 1];
            opp.Subscription_Status__c = subStatus;
            update opp;
        }
        
        if(buyerId != null  && oppId == null){
            System.debug('buyerId  ' +buyerId);
            Account acc = [SELECT ID,Name,API_Buyer_Id__c FROM Account WHERE API_Buyer_Id__c =:buyerId LIMIT 1];
            System.debug('Acc' +acc);
            Id rtId = Schema.SObjectType.Opportunity.getRecordTypeInfosByName().get('Buyer Development Opportunity').getRecordTypeId();
            Opportunity opp =new Opportunity();
            opp.name = acc.Name;
            opp.AccountId = acc.Id;
            opp.StageName = 'Closed Won';
            opp.Subscription_Status__c = subStatus;
            opp.CloseDate = System.today();
            opp.RecordTypeId = rtId;
            opp.Subscription_ID__c  = subsId;
            opp.LeadSource= '';
            insert opp;
            Product2 prd = new Product2();
            if(subsId == '1'){               
                prd = [SELECT ID,Name FROM Product2 WHERE Name = 'Vehicle Score' LIMIT 1];
                Opportunity opp1 = [Select id, name , Subscription_Name__c from Opportunity where id =: opp.Id ];
                
                opp1.Subscription_Name__c = prd.Name;
                opp1.Name = acc.Name+'-'+opp1.Subscription_Name__c+'-'+opp1.id;
                update opp1;
                
            }
            else if(subsId == '2'){
                prd = [SELECT ID,Name FROM Product2 WHERE Name = 'Custom Bid' LIMIT 1];
                Opportunity opp1 = [Select id, name , Subscription_Name__c from Opportunity where id =: opp.Id ];
                opp1.Subscription_Name__c = prd.Name;
                opp1.Name = acc.Name+'-'+opp1.Subscription_Name__c+'-'+opp1.id;
                update opp1;
               
            }
            
        }
    }
}
 
Test Class :-

@isTest
public class subscriptionUpdateTest {
    
    @isTest static void testPostMethod(){
        Test.startTest();
        Account acc = new Account();
        acc.Name = 'Test Account';
        acc.API_Buyer_Id__c = '123456';
        insert acc;
        Opportunity TestOpp = new Opportunity();
        date mydate = date.parse('12/27/2022');
        TestOpp.name = 'TestOpp1';
        TestOpp.StageName = '0. Qualifying';
        TestOpp.CloseDate = mydate;
        TestOpp.AccountId = acc.Id;
        TestOpp.Subscription_Status__c = 'Subscribed';
        insert TestOpp;
        Opportunity opp = [SELECT ID,Buy_ID__c,AccountId,Account.API_Buyer_Id__c FROM Opportunity WHERE id = :TestOpp.Id];
        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();
        
        req.requestURI = '/services/apexrest/subscriptionUpdate/';
       String JSONMsg = '{"opportunityid" : "'+opp.Id+'", "buyerId" : "'+opp.Account.API_Buyer_Id__c+'","Subscription Status":"Subscribed", "SubscriptionId" : "1"}';
        //String JSONMsg = '{"opportunityid" : "null", "Subscription Status":"Subscribed", "SubscriptionId" : "1"}';
        req.requestBody = Blob.valueof(JSONMsg);
        req.httpMethod = 'POST';
        RestContext.request = req;
        RestContext.response= res;
        subscriptionUpdate.doPost();
        
        Test.stopTest();
        
    }
}

Any Suggestion would be really appreciated !!! for leting me know where I am doing wrong due to which its show very less coverage.
 
SwethaSwetha (Salesforce Developers) 
HI Sahil,
The provided code does not highlight which lines of code are uncovered. The below information should help you get started.
 
https://salesforce.stackexchange.com/questions/244788/how-do-i-write-an-apex-unit-test

https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines 

Thanks