0
@RestResource(urlMapping='/Account/*')
global with sharing class MyRestResource {

    @HttpDelete
    global static void doDelete() {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        String accountId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
        Account account = [SELECT Id FROM Account WHERE Id = :accountId];
        delete account;
    }

    @HttpGet
    global static Account doGet() {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        String accountId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
        Account result = [SELECT Id, Name, Phone, Website FROM Account WHERE Id = :accountId];
        return result;
    }

  @HttpPost
    global static String doPost(String name,
        String phone, String website) {
        Account account = new Account();
        account.Name = name;
        account.phone = phone;
        account.website = website;
        insert account;
        return account.Id;
    }

     @HttpPatch
    global static String doPatch(String name,
        String phone, String website) {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        String accountId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
        Account account = [SELECT Id,name,phone FROM Account WHERE Id = :accountId];
        account.Name = name;
        account.phone = phone;
        update account;
        return account.Id;


    }
}

1 Answer 1

1

As Salesforce tests the deserialization/serialization, all you need to test is the Apex code. So from your test methods you setup the static RestContextand then directly invoke the class methods e.g.:

RestRequest req = new RestRequest();
req.requestURI = ...;
RestContext.request = req;

String id = MyRestResource.doPatch('Acme', '123 45678', 'http://bbc.co.uk');

// Assert the results

You must log in to answer this question.

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