function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Tena WolverTena Wolver 

Test Webservice where I use many Webservice Methods

I have to test a webservice where I first pull session informaton and then I pull down data by calling another method.  This is all done in one method in my actual class.  I have created 2 mockup classes to use with testing.
global class serverGetSessionMockImpl implements WebServiceMock {infoi inside for Session mockup}
global class serverGetClassesMockImpl implements WebServiceMock {info inside for Classes mock up}

In my test class I call these classes.
   Test.setMock(WebServiceMock.class, new serverGetSessionMockImpl());
    //setup the class information
    Test.setMock(WebServiceMock.class, new serverGetClassesMockImpl());
I then call the class method that uses this webservice in my test class.
     //set page to the controller
     myPageCon.LoadPage();  //this actually uses the webservice method
What I find is that whatever was the last mock service class I called(Session or Classes) is the one that gets used in my test.  I need it to use each of them.

How do I do this?
James LoghryJames Loghry
You'll need to provide a mechanism in your mock callout class to return multiple responses.  Below is an example I have used in the past:


@isTest
global class MyTestMockCallout implements HttpCalloutMock{

    private List<String> responses {get; private set;}
    private boolean isExceptionThrown {get; private set;}

    //For handling multiple callout cases 
    //We will use a List of responses and this static index variable to keep
    //track of which response we need to send back.
    public static Integer TEST_CALLOUT_INDEX = 0;

	global MyTestMockCallout(List<String> responses, boolean isExceptionThrown){
		this.responses = responses;
		this.isExceptionThrown = isExceptionThrown;
	}

	global HttpResponse respond(HttpRequest req){
		if(isExceptionThrown){
			throw new CustomException('Exception thrown');
		}

		HttpResponse resp = new HttpResponse();
		resp.setStatusCode(200);
		resp.setStatus('TEST');
		resp.setBody(this.responses.get(TEST_CALLOUT_INDEX));

		//Increment Callout Index for multiple callout processing in a single unit test.
		TEST_CALLOUT_INDEX++;
		return resp;
	}
}


James LoghryJames Loghry
Note, that in my example, you would construct the callout with a list of response strings (one for each of the requests in your method).  Also, the class has the ability for testing callout exceptions.  (Passing in true to the second parameter in the constructor will throw an exception)
Tena WolverTena Wolver
I guess I am not getting it.  Where does the code for my mock webservice go?  How does it know which one to use?  Sorry this is going over the head and below the feet :s

global class serverGetSessionMockImpl implements WebServiceMock {infoi inside for Session mockup}
global class serverGetClassesMockImpl implements WebServiceMock {info inside for Classes mock up}