1

I tried many link for creating test class for Rest Api, but still not getting the value.

My class

@RestResource(urlMapping='/claimApi')
global with sharing class UserClaimApi {
    @HttpGet
    global static User getClaimUser() {
        List<User> userList = new List<User>();
        String phoneNumber = RestContext.request.params.get('phoneNumber');
        String claimNumber = RestContext.request.params.get('claimNumber');
        System.debug('phone number'+phoneNumber);

        if(phoneNumber != null || phoneNumber != '' && claimNumber == null || claimNumber == '') {
            List<Contact> contactList = [Select Id from Contact where Phone_Numbers_for_Search__c =: phoneNumber];
            if(contactList.size() == 1) {
                List<Claim_Contact__c> claimContactList = [Select Claim__c from Claim_Contact__c where Contact__c =: contactList[0].id];

                if(claimContactList != null && claimContactList.size() > 0) {
                    System.debug(claimContactList[0]);
                    List<Claim__c> claimList = [Select OwnerId from Claim__c where id =: claimContactList[0].Claim__c];
                    if(claimList.size() > 0) {
                        User claimUser = [Select Email,Name,Team__c,Overflow_Team__c from User where Id =: claimList[0].OwnerId];
                        userList.add(claimUser);
                    }
                }else {
                    return null;
                }
            }else {
                return null;
            }
        }else if(claimNumber != null && claimNumber != '') {
            List<Claim__c> claimList = [Select OwnerId from Claim__c where Claim_No__c =: claimNumber];
            if(claimList.size() > 0) {
                User claimUser = [Select Email,Name,Team__c,Overflow_Team__c from User where Id =: claimList[0].OwnerId];
                userList.add(claimUser);
            }
        } else {
            return null;
        }
        return userList[0];
    }
}

Test Class:

@isTest
public class UserClaimApiTest {
    public static testMethod void getPhoneUser() {
        Profile profileId = [SELECT Id FROM Profile WHERE Name = 'Standard User' LIMIT 1];

        Contact con = new Contact();
        con.lastName = 'test';
        con.Email = 'test@gmail.com.com';
        con.Phone_Numbers_for_Search__c = '9876543210';
        insert con;

        User usr = new User(LastName = 'Test',
            FirstName='EML',
            Alias = 'EmlT',
            Email = 'eml.test@gmail.com',
            Username = 'eml.test@gmail.com',
            ProfileId = profileId.id,
            TimeZoneSidKey = 'GMT',
            LanguageLocaleKey = 'en_US',
            EmailEncodingKey = 'UTF-8',
            LocaleSidKey = 'en_US'
            );
        insert usr;

        Claim__c claim = new Claim__c();
        claim.Claim_No__c = 'C-12345';
        insert claim;

        Claim_Contact__c claimContact = new Claim_Contact__c();
        claimContact.Contact__c = con.id;
        claimContact.Claim__c = claim.id;
        insert claimContact;

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

        req.requestURI = '/services/apexrest/claimApi'; 
        req.addParameter('phoneNumber', '9876543210');
        req.httpMethod = 'GET';
        RestContext.request = req;
        RestContext.response = res;



        User usr1 = UserClaimApi.getClaimUser();
        System.debug(usr1);
    }
}

Not getting value of phoneNumber and claimNumber. My question is almost similar to the solution of Test class Rest Webservice parameters . In my case that won't worked, and the answer suggested by NITHESH K was right.

2
  • Almost similar to what you have suggested. Sep 1, 2017 at 7:41
  • @MarkPond I think that this is already what she's doing. Sep 1, 2017 at 8:48

1 Answer 1

4

Try with below code, May the reason you missed content-type i guess and the way of storing params value.

    Test.startTest();
        RestRequest req = new RestRequest(); 
        RestResponse res = new RestResponse();
        req.requestURI = '/services/apexrest/claimApi'; 
        req.params.put('phoneNumber', '9876543210');
        req.params.put('claimNumber', '111111');
        req.httpMethod = 'GET';
        req.addHeader('Content-Type', 'application/json'); 
        RestContext.request = req;
        RestContext.response = res;
        User usr1 = UserClaimApi.getClaimUser();
        System.debug(usr1);
   Test.stopTest();   

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