1

Why would I get an 500 error on this test class? I am troubleshooting someone else's test class and am only getting five out of nine methods passing with all four failing methods failing on the assertequals (System.assertEquals(res.statusCode, 200) (e.g. line 290, almost to bottom of the test class). 

​Any help would be appreciated.

    @isTest
public class QB_UpdateAccountTest {

    static RestRequest req = new RestRequest(); 
    static RestResponse res = new RestResponse();
    static blob b;
    static string suid;

    static private void setUpsert(boolean upsertOnNoMatch){
        API_Settings__c APISettings = API_Settings__c.getInstance('Update Account API');
        APISettings.Insert_Account_When_Not_Found__c = upsertOnNoMatch;
        Database.update(APISettings);
    }

    static private void setupData(){
        // setup the request 
        req.httpMethod = 'PATCH';
        req.requestURI = '/services/apexrest/QB_UpdateAccount';   
        RestContext.request = req;
        RestContext.response = res;        
        Id recordTypeId;
        Id CrecordTypeId;

        recordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Contractor').getRecordTypeId();
        CrecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Operator').getRecordTypeId();

        // setup the settings
        System.Debug ('setting data');
        API_Settings__c setting = new API_Settings__c();
        setting.name = 'Update Account API';
        setting.Insert_Account_When_Not_Found__c = false;
        setting.admin_id__c = UserInfo.getUserId();
        setting.additionalText1__c = 'DunsNumber';
        setting.additionalText2__c = 'sic';
        setting.additionalDate1__c = 'Health_Modified_Date__c';
        setting.additionalBoolean1__c = 'IsBillable__c';
        Database.insert(setting);

        Account a1WithSuid = new Account();
        Account a2WithOrgId = new Account();
        Account boeing = new Account();
        Account slalom = new Account();
        Account ph1111 = new Account();

        a1WithSuid.name = 'withSUID';
        a1WithSuid.RecordTypeId = recordTypeId;
        a1WithSuid.status__c = 'Prospect';

        a2withOrgId.name = 'OrgId:1234';
        a2withOrgId.Organizer_Id__c = '1234';
        a2withOrgId.RecordTypeId = CrecordTypeId;
        a2withOrgId.status__c = 'Active';

        boeing.name = 'boeing Inc';
        boeing.RecordTypeId = CrecordTypeId;
        boeing.status__c = 'Prospect';

        slalom.Website = 'www.slalom.com';
        slalom.name = 'slalom';
        slalom.RecordTypeId = recordTypeId;
        slalom.status__c = 'Prospect';

        ph1111.name = '111-111-1111';
        ph1111.phone = '111-111-1111';
        ph1111.RecordTypeId = recordTypeId;
        ph1111.status__c = 'Prospect';

        Database.insert(a1WithSuid);
        Database.insert(a2WithOrgId);  
        Database.insert(boeing);
        Database.insert(slalom);
        Database.insert(ph1111);

        a1WithSuid = [select id, name, suid__c from account where id = :a1WithSuid.id];
        suid = a1WithSuid.suid__c;

        system.debug ('a1WithSuid' + a1WithSuid.SUID__c);
        system.debug ('a2WithOrgId' + a2WithOrgId);
        system.debug ('boeing' + boeing);
        system.debug ('slalom' + slalom);
        system.debug ('ph1111' + ph1111);
    }

    static testmethod void updateAccountErrors(){ 
        // setup data for offices
        setupData();

        //pass the req and resp objects to the method     

        QB_UpdateAccount.AccountLight al = new QB_UpdateAccount.AccountLight();

        // no account type passed
        QB_UpdateAccount.SFDC_UpdateAccount(al);
        System.assertEquals(res.statusCode, 400);
        system.debug ('respone' + res);
        b = res.responseBody;     
        System.debug( 'b.toString: ' + b.toString());
        System.assert(b.toString().contains('Invalid Account Type'));

    } 

    static testmethod void updateAccountWithOrgId(){ 
        // setup data for offices
        setupData();

        System.debug('TEST #2... UPDATE WITH ORGANIZER ID');
        //pass the req and resp objects to the method     

        // match on a2withOrgid
        QB_UpdateAccount.AccountLight al = new QB_UpdateAccount.AccountLight();
        al.AccountRecordType = 'Client';
        al.OrganizerId = '1234';
        al.street = '1 Coloma';
        al.city = 'Irvine';
        al.state = 'CA';
        al.postalCode = '92602';
        al.country = 'USA';


        QB_UpdateAccount.SFDC_UpdateAccount(al);
        b = res.responseBody;
        System.debug ('*****DESERIALIZE B'+ b.toString());
        Account a = (Account) JSON.deserialize(b.toString(), Account.class);
        system.debug('Account:' + a);
        System.assert(a.name.contains('OrgId:1234'));
        System.assertEquals(res.statusCode, 200);
    }

    static testmethod void updateAccountWithSUID(){ 
        // setup data for offices
        setupData();

        System.debug('TEST #3... UPDATE WITH SUID');
        //pass the req and resp objects to the method     

        QB_UpdateAccount.AccountLight al = new QB_UpdateAccount.AccountLight();
        al.AccountRecordType = 'Supplier';
        al.SUID = suid;
        al.street = '1 Coloma';
        al.city = 'Irvine';
        al.state = 'CA';
        al.postalCode = '92602';
        al.country = 'USA';

        al.additionalText1 = 'Duns1';
        al.additionalText2 = 'additionalText2';
        al.additionalDate1 = '2016-11-20';
        al.additionalBoolean1 = true;

        system.debug('Before Executing TEST #3:');

        QB_UpdateAccount.SFDC_UpdateAccount(al);
        b = res.responseBody;
        System.assertEquals(res.statusCode, 200);
        Account a = (Account) JSON.deserialize(b.toString(), Account.class);
        system.debug('Account:' + a);
        System.assert(a.name.contains('withSUID'));

    } 

    static testmethod void updateAccountWithName(){ 
        setupData();

        System.debug('TEST #4... UPDATE WITH name match - Boeing');

        QB_UpdateAccount.AccountLight al = new QB_UpdateAccount.AccountLight();
        al.AccountRecordType = 'Client';
        al.accountName = 'BOEING Inc.';
        al.street = '1 Coloma';
        al.city = 'Irvine';
        al.state = 'CA';
        al.postalCode = '92602';
        al.country = 'USA';
        al.parentAccountId = '1234';
        al.globalAccountId = '1234';
        al.additionalText1 = 'Duns1';
        al.additionalText2 = 'additionalText2';
        al.additionalDate1 = '2016-11-20';
        al.additionalBoolean1 = true;

        QB_UpdateAccount.SFDC_UpdateAccount(al);
        b = res.responseBody;
        System.assertEquals(res.statusCode, 200);
        Account a = (Account) JSON.deserialize(b.toString(), Account.class);
        system.debug('Account:' + a);
        System.assert(a.name.contains('BOEING'));

    } 

    static testmethod void updateAccountWithURL(){ 
        setupData();

        System.debug('TEST #5... UPDATE WITH name URL.');
        //This should not match on name, but match url.  

        QB_UpdateAccount.AccountLight al = new QB_UpdateAccount.AccountLight();
        al.AccountRecordType = 'Supplier';
        al.accountName = 'SLALOM COMPANY';
        al.webUrl = 'www.slalom.com';
        al.street = '1 Coloma';
        al.city = 'Irvine';
        al.state = 'CA';
        al.postalCode = '92602';
        al.country = 'USA';

        QB_UpdateAccount.SFDC_UpdateAccount(al);
        b = res.responseBody;
        System.assertEquals(res.statusCode, 200);
        Account a = (Account) JSON.deserialize(b.toString(), Account.class);
        system.debug('Account:' + a);
        System.assert(a.name.contains('SLALOM COMPANY'));
    } 
    static testmethod void updateAccountNoMatchNoInsert(){ 
        setupData();
        setUpsert (false);

        System.debug('TEST #6... No match and no Insert.');
        //This should not match on name, but match url.  

        QB_UpdateAccount.AccountLight al = new QB_UpdateAccount.AccountLight();
        al.AccountRecordType = 'Supplier';
        al.accountName = 'NO MATCH';
        al.webUrl = 'www.nomatch.com';
        al.street = '1 Coloma';
        al.city = 'Irvine';
        al.state = 'CA';
        al.postalCode = '92602';
        al.country = 'USA';

        QB_UpdateAccount.SFDC_UpdateAccount(al);
        System.assertEquals(res.statusCode, 400);
    } 

    static testmethod void updateSupplierAccountNoMatchWithInsert(){ 
        setupData();
        setUpsert (true);

        System.debug('TEST #7... No match but Insert.');
        //This should not match on name, and do insert

        QB_UpdateAccount.AccountLight al = new QB_UpdateAccount.AccountLight();
        al.AccountRecordType = 'Supplier';
        al.accountName = 'NO MATCH SUPPLIER';
        al.webUrl = 'www.nomatchsupplier.com';
        al.street = '1 Coloma';
        al.city = 'Irvine';
        al.state = 'CA';
        al.postalCode = '92602';
        al.country = 'USA';
        al.billable = true;
        al.additionalText1 = 'Duns1';
        al.additionalText2 = 'additionalText2';
        al.additionalDate1 = '2016-11-20';
        al.additionalBoolean1 = true;

        QB_UpdateAccount.SFDC_UpdateAccount(al);
        b = res.responseBody;
        System.assertEquals(res.statusCode, 200);
        Account a = (Account) JSON.deserialize(b.toString(), Account.class);
        system.debug('Account:' + a);
        System.assert(a.name.contains('NO MATCH'));
    }  

    static testmethod void updateClientAccountNoMatchWithInsert(){ 
        setupData();
        setUpsert (true);

        System.debug('TEST #8... No match but Insert Client.');
        //This should not match on name, and do insert

        QB_UpdateAccount.AccountLight al = new QB_UpdateAccount.AccountLight();
        al.AccountRecordType = 'Client';
        al.accountName = 'NO MATCH Client';
        al.webUrl = 'www.nomatchClient.com';
        al.street = '1 Coloma';
        al.city = 'Irvine';
        al.state = 'CA';
        al.postalCode = '92602';
        al.country = 'USA';
        al.billable = true;
        al.parentAccountId = '1234';
        al.globalAccountId = '1234';
        al.additionalText1 = 'Duns1';
        al.additionalText2 = 'additionalText2';
        al.additionalDate1 = '2016-11-20';
        al.additionalBoolean1 = true;

        QB_UpdateAccount.SFDC_UpdateAccount(al);
        b = res.responseBody;
        System.assertEquals(res.statusCode, 200);
        Account a = (Account) JSON.deserialize(b.toString(), Account.class);
        system.debug('Account:' + a);
        System.assert(a.name.contains('NO MATCH'));
        System.assertEquals(res.statusCode, 200);
    }  

    static testmethod void GetAccountInfoTest(){ 
        setupData();

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

        req.httpMethod = 'GET';
        req.requestURI = '/services/apexrest/account?';   
        RestContext.request = req;
        RestContext.response = res; 

        QB_UpdateAccount.GetAccountInfo();
        System.assertEquals(400, res.statusCode);

        req.addParameter ('orgid', '1234');
        QB_UpdateAccount.GetAccountInfo();
        System.assertEquals(200, res.statusCode);


        }

}

2 Answers 2

3

When testing a @RestResource, simply call the method directly.

RestContext.request = new RestRequest();
RestContext.response = new RestResponse();
// set parameters as desired in request object
MyRestServiceClass.httpPostMethod();
0
1

You have to create Mock callouts for your outbound testing purpose.

global class YourHttpCalloutMockImpl implements HttpCalloutMock {
    global HTTPResponse respond(HTTPRequest req) {
        // Create a fake response.
        // Set response values, and 
        // return response.
    }
}

Then use, Test.setMock(HttpCalloutMock.class, new YourHttpCalloutMockImpl());

For more information, refer Testing HTTP Callouts by Implementing the HttpCalloutMock Interface

For inbound to Salesforce testing, you have to create appropriate JSON structure to send the request.

1
  • Will this Interface work with a situation where it is an apex class exposed as an inbound HTTP Rest Service? Feb 5, 2018 at 19:05

You must log in to answer this question.

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