1

I am new to apex and have some questions.

Below are my conditions.

I have class A_Class in which it calls the external end point in some methods.

To test this class, I have already written HTTP mock class and succesfully tested.

I have a schedular class, which calls A_Class to start to run its main method.

I want to test that schedular class.

But when I directly run the schedular with test class, it hits exception because of [TestMethod do not support Web service callouts].

I successfully tested A_Class with HTTP mock class, now I only want to test my schedular whether it is running at sepcific time correctly.

Do I need to comment out the calling of A_Class from schedular when I run test class? If I comment it out, just afraid it gives me problem when I deploy to Production.

How should I approach that kind of situation?

Thanks ahead. Any hints or ideas will be so useful for me.

---Updated (after test)---

(main call method)
Map<String,Object> result = new Map<String,Object>();

        if(Test.isrunningtest())
        {
            // Set mock callout class
            Test.setMock(HttpCalloutMock.class, new Test_Class_Mock_Job_RSS());      
        }

        HTTPResponse res = callOutRssJobFeeds(url);
        result = getJobRSS(res); 

public HTTPResponse callOutRssJobFeeds(String url)
    {
        HttpRequest req = new HttpRequest();
        req.setMethod('GET');
        req.setEndpoint(url);

        Http http = new Http();
        HTTPResponse res = http.send(req);
        return res;
    }

Now I am getting [System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out] after updated like above and run the test class.

1

Either set up the scheduler class to ignore the function call while testing (via if(Test.isrunningtest()) { return; }, or set up the mock callout, as you did for the other. You may want to just encapsulate the mock callout function in a utility class that contains test methods for testing your code.

  • Hi for now, like your suggested (not exactly same though).. I pass parameter (test=true/false) to skip the real call to external point. I will try ur approach. If have issues, I will let you know. Thanks – kitokid Jul 18 '13 at 2:21
  • Hi sfdcfox, can you take a look my updated approach. Now I am getting [System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out] . – kitokid Jul 18 '13 at 2:53
  • Plus there is no DML statements before the http call out. only getting some values from custom settings. – kitokid Jul 18 '13 at 3:38
  • Try moving your Test.startTest() to just before you try to schedule/call your class. – sfdcfox Jul 18 '13 at 4:09
  • I put Test.setMock(HttpCalloutMock.class, new Test_Class_Mock_Job_RSS()); between Test.startTest() and Test.endTest().. but only get [Method only allowed during testing] error. – kitokid Jul 18 '13 at 5:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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