0

//Help me to write test class for this class.

@RestResource(urlMapping='/updateCaseStatus/*')

global class HttpMethodsJiraToSF
{

    @HttpPost   
    global static void getvaluefromjira() {
        try{   

                RestRequest request = RestContext.request;

                RestResponse res = RestContext.response;
                // system.debug('--------------'+request.requestBody.toString());
                String jSONRequestBody=request.requestBody.toString().trim();  
                system.debug('--------------'+request.requestBody.toString());
                system.debug('--------------'+jSONRequestBody);

                  HttpMethodsJiraToSFRes.jira resJira = (HttpMethodsJiraToSFRes.jira)JSON.deserialize(jSONRequestBody,HttpMethodsJiraToSFRes.jira.class);

                    system.debug('--------------'+resJira.caseid);
                    system.debug('--------------'+resJira.Status);

                    case c=[select id,status from case where id =:resJira.caseid];

                     if(resJira.Status=='y')
                         c.status=resJira.Status;

                    update c;

           }catch(Exception  x)
           {
            system.debug('--------------'+x);   
              return;     
           } 

    }
}
3
  • Have u tried this? salesforce.stackexchange.com/questions/24984/… Commented Nov 23, 2015 at 13:48
  • Yes I tried @susanoo .Problem at how to deserialize request.requestBody. Commented Nov 23, 2015 at 13:51
  • What have you written so far? For a REST based class, in my experience you need to create a Mock object. Also in the class itself, you need to add a conditional IsTestRunning if condition.
    – abhi
    Commented Nov 23, 2015 at 14:23

1 Answer 1

1

As you are explicitly deserializing, you will need to do the corresponding serialize in your test:

@isTest
static void yourTestMethod() {

    Case c = new Case(...);
    insert c;

    HttpMethodsJiraToSFRes.jira body = new HttpMethodsJiraToSFRes.jira();

    // Fill in fields of body here e.g.:
    body.caseid = c.Id;
    body.Status = 'y';

    RestRequest req = new RestRequest();
    req.httpMethod = 'POST';
    req.requestUri = '...';
    req.requestBody = Blob.valueOf(JSON.serializePretty(body));
    RestContext.request = req;
    RestContext.response = new RestResponse();

    HttpMethodsJiraToSF.getvaluefromjira();

    // Add asserts here
}

The test sets the RestContext global fields that the target @RestResource class then uses.

3
  • case c=[select id,status from case where id =:resJira.caseid]; This query getting none. All resJira values are not matching.What I required I had given those but unable to verify condition also at Status. Commented Nov 23, 2015 at 15:29
  • @HARIKRISHNA Tests are isolated from data that is already in the org so you need to insert a case as part of your test; I've added a couple of lines to the start of the test method to illustrate where,
    – Keith C
    Commented Nov 23, 2015 at 16:34
  • Thanks Keith.It s really helped me.I am unable to give rank you. Commented Nov 24, 2015 at 4:33

You must log in to answer this question.

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