-3

Looking for some help here. I'm trying to write a Test Class for a RestResource I defined but am receiving the above Test Class failure. I was under the impression that you didn't need to define a Mock class for Rest Resources.

Can someone help out? I'm really scratching my head on this one Side note: My method is a POST method when it is really doing a GET. I need to go against convention because the system making the call is only able to make POST requests in our scenario

RestResource Class:

@RestResource(urlMapping='/cases/*')
global class CaseRestResource {
        @HttpPost
    global static void getCaseInfo(){
         RestResponse res = RestContext.response;
         RestRequest req = RestContext.request;
         String caseNum = String.valueOf(req.params.get('text'));
        System.debug(caseNum);
        try{
            Case[] foundCases = [SELECT Id, CaseNumber, Subject, Account.Name, Owner.Name from Case WHERE CaseNumber = :caseNum LIMIT 1];
            if(foundCases.size() > 0){
                String caseOwner;
                String caseSubject;
                String caseClient;
                
                caseOwner = foundCases[0].Owner.Name;
                caseSubject = foundCases[0].Subject;
                caseClient = foundCases[0].Account.Name;
                
                res.addHeader('Content-Type','application/json');
                res.responseBody = Blob.valueOf('{'+
    '"blocks": ['+
        '{'+
            '"type": "section",'+
            '"text": {'+
                '"text": "Here is the information about the case you requested.  The case is owned by *'+caseOwner+'*",'+
                '"type": "mrkdwn"'+
            '},'+
            '"fields": ['+
                '{'+
                    '"type": "mrkdwn",'+
                    '"text": "*Client Name*"'+
                '},'+
                '{'+
                    '"type": "mrkdwn",'+
                    '"text": "*Case Subject*"'+
                '},'+
                '{'+
                    '"type": "mrkdwn",'+
                    '"text": "'+caseClient+'"'+
                '},'+
                '{'+
                    '"type": "plain_text",'+
                    '"text": "'+caseSubject+'"'+
                '}'+
            ']'+
        '}'+
    ']'+
'}'); 
            }
            else{
            res.addHeader('Content-Type','application/json');
            res.responseBody = Blob.valueOf('{"Error":"Error, nothing found"}');
            }
        }catch(Exception e){
            res.addHeader('Content-Type','application/json');
            res.responseBody = Blob.valueOf('{"Error":"Error, nothing found,:::'+e+'"}');
        }
    }
}

Test Class:

@isTest
public class TestCaseRestResource{
    @isTest
    public static void testGetCaseInfo(){
        // Prepare test Data
        Case c1 = new Case();
        insert c1;
        
        // Initialize Rest Context
        RestRequest req = new RestRequest();
        RestResponse res = new RestResponse();
        req.requestURI = '/services/apexrest/cases';
        req.params.put('text',c1.CaseNumber);
        req.httpMethod = 'POST';
        req.addHeader('Content-Type', 'application/json');
        RestContext.request = req;
        RestContext.response = res;
        Test.startTest();
        CaseRestResource.getCaseInfo();
        Test.stopTest();
    }
}

Thanks

0

1 Answer 1

0

In case this helps someone else, I was able to figure out my answer. In my initial question, I was creating my own test data:

Case c1 = new Case();
insert c1;

And then using that data in my parameters:

req.params.put('text',c1.CaseNumber);

That is what gave me the error "Methods defined as TestMethod do not support Web service callouts."

Instead of creating and inserting my own data, I instead resorted to (seeAllData=true) in my test method, and queried the "test" data directly from the org.

Case c1 = [SELECT Id, CaseNumber FROM Case LIMIT 1][0]; Test passed with no issues. Not something I knew, but seems like the DML operation gave the test method the interpretation I was trying to make a service callout instead of just testing my RestResource

3
  • 3
    seeAllData=true is not required; the querying for CaseNumber though is
    – cropredy
    Apr 11 at 23:52
  • @cropredy seeAllData is required, as I am now querying data directly from the org. Without that annotation, my list would be of length 0 because I am no longer creating any case records within the method with which to test Apr 11 at 23:54
  • 2
    the testmethod mocks the Case within the test context
    – cropredy
    Apr 11 at 23:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.