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
zeenat mangatzeenat mangat 

please help me to improve my test class, now it is 72% in the production

Here is my apex class controller.
Below this I have created its test class.
please help me to find the problem.

@RestResource(urlMapping='/v2/addUpsell/*')
global with sharing class REST_Upsell_Controller {
       
    @HttpPost
    global static String processUpsell(String orderId, Decimal price, Decimal shipping, Decimal tax){       
  
        ChargentOrders__ChargentOrder__c order = [SELECT Id, ChargentOrders__Subtotal__c, ChargentOrders__Tax__c, ChargentOrders__Shipping__c FROM ChargentOrders__ChargentOrder__c WHERE Id = :orderId];
        
        
        Decimal prc = order.ChargentOrders__Subtotal__c;
        Decimal tx = order.ChargentOrders__Tax__c;
        Decimal shp = order.ChargentOrders__Shipping__c;
        
        if(tx!= null){
            order.ChargentOrders__Tax__c= tx+tax;
        }else{
            order.ChargentOrders__Tax__c= tax;
        }
        
        if(shp!= null){
            order.ChargentOrders__Shipping__c= shp+shipping;
        }else{
            order.ChargentOrders__Shipping__c= shipping;
        }
        
        order.ChargentOrders__Subtotal__c= prc+price;
                        
        try {
        
            /* Update Order with Upsell Amount */
            update  order;           
            return 'added';   
        } catch (DmlException e) {
             
            return 'false';
        }
    }   
}



Test class :-

@isTest(SeeAllData=true)
public class REST_Upsell_Controller_Test {
    static testMethod void testCreateNewUpsell(){
        String orderId = 'a0555000000N5BnAAK';
        Decimal price = 30.00;
        Decimal shipping = 20.55;
        Decimal tax = 12.22;
        
        Decimal prc = 10.00;
        Decimal tx = 20.22;
        Decimal shp = 15.20;
        
        Decimal txs = tx+tax;
        Decimal shps = shp+shipping;
        Decimal prcs = prc+price;
        ChargentOrders__ChargentOrder__c oderobj = new ChargentOrders__ChargentOrder__c();
        oderobj.ChargentOrders__Tax__c = txs;
        oderobj.ChargentOrders__Shipping__c = shps;
        oderobj.ChargentOrders__Subtotal__c = prcs;
        insert oderobj;
        
        RestRequest req = new RestRequest();
        RestResponse res = new RestResponse();

        req.requestURI = '/v2/addUpsell/*';  //Request URL
        req.httpMethod = 'POST';//HTTP Request Type
        RestContext.request = req;
        RestContext.response= res;
        
        String strResponse = REST_Upsell_Controller.processUpsell('a0555000000N5BnAAK',30.00,20.55,12.22);
        System.assertEquals('added',strResponse);
    }
}
RAM AnisettiRAM Anisetti
Hi,
try this one...

 
@isTest(SeeAllData=true)
public class REST_Upsell_Controller_Test {
    static testMethod void testCreateNewUpsell(){
        
        
   //create record 

   ChargentOrders__ChargentOrder__c corder=new ChargentOrders__ChargentOrder__c();
   corder.ChargentOrders__Subtotal__c=30.00;
   corder.ChargentOrders__Tax__c=20.55; 
   corder.ChargentOrders__Shipping__c=12.22;
   
   insert corder;

       
        
        RestRequest req = new RestRequest();
        RestResponse res = new RestResponse();

        req.requestURI = '/v2/addUpsell/*';  //Request URL
        req.httpMethod = 'POST';//HTTP Request Type
        RestContext.request = req;
        RestContext.response= res;
        
        String strResponse = REST_Upsell_Controller.processUpsell(corder.id,30.00,20.55,12.22);
        System.assertEquals('added',strResponse);
    }

  static testMethod void testCreateNewUpsell2(){
        
        
   //create record 

   ChargentOrders__ChargentOrder__c corder1=new ChargentOrders__ChargentOrder__c();
   corder.ChargentOrders__Subtotal__c=null;
   corder.ChargentOrders__Tax__c=null; 
   corder.ChargentOrders__Shipping__c=null;
   
   insert corder1;

       
        
        RestRequest req = new RestRequest();
        RestResponse res = new RestResponse();

        req.requestURI = '/v2/addUpsell/*';  //Request URL
        req.httpMethod = 'POST';//HTTP Request Type
        RestContext.request = req;
        RestContext.response= res;
        
        String strResponse = REST_Upsell_Controller.processUpsell(corder1.id,30.00,20.55,12.22);
        System.assertEquals('added',strResponse);
    }




}