Here is my apex class, I would like to create the test class of it so that its coverage code must be 100%.Can anyone help me to create its test class.


global with sharing class REST_Upsell_Controller {

    global static String processUpsell(String orderId){       

        ChargentOrders__ChargentOrder__c order = [SELECT Id, ChargentOrders__Charge_Amount__c, ChargentOrders__Tax__c, ChargentOrders__Subtotal__c FROM ChargentOrders__ChargentOrder__c WHERE Id = :orderId];
        order.ChargentOrders__Subtotal__c = 600.66;
        try {
            update  order;
            return 'Upsell Added'; 
        } catch (DmlException e) {

            return 'Upsell Not Added';
  • 1
    I don't see any Restrequest or Restresponse in your class you need to cover all those to have 100% code coverage. Aug 29, 2015 at 14:40

1 Answer 1


What I have below is not 100% since it doesn't cover the update failing, and it also doesn't have any system.assert() methods, so it isn't even a proper test, but this should help get you started at least.

public class TestRest{

    public static void insertAnOrder(){
        Account a = new Account(name='AnAccount');
        insert a;
        insert new Order(name='AnOrder', AccountId=a.Id, EffectiveDate=system.today(), Status='Draft');

    public static @isTest void testRest(){
        Id OrderId = [SELECT Id FROM Order LIMIT 1].Id;
        System.RestContext.request = new RestRequest();
        RestContext.request.requestURI = '/v2/getUpsell/';


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .