I have a web service callout that checks if an order’s type is return and needs to see what the actual boughtPrice is:

webservice static String updateBoughtPrice(Id ordId){
  Decimal boughtPrice = Orders.grabBoughtPrice(order.Buyer__r.email);

 if (order.Type__c == ‘return'){
            if(boughtPrice == null){
                return ‘boughtPrice is null';
            Decimal updateBoughtAmount = boughtPrice - order.Total__c;
            if(updateBoughtAmount < 0){
                return ‘Bought price cannot be negative';
            else {
                if(boughtPrice!= null){
                    boughtPrice = updateBoughtAmount;

boughtPrice is brought from an api callout in my Orders Class:

 Decimal boughtPrice = Orders.grabBoughtPrice(order.Buyer__r.email);

 public static decimal grabBoughtPrice(string email){
    Decimal boughtPrice;
    String endpoint ='endpoint url here';
    String token = 'token';
    Http http=new Http();
    HttpRequest request=new HttpRequest();
    request.setHeader('auth', token);
    request.setHeader('Content-Type', 'application/json');
    HttpResponse response=http.send(request);
        boughtprice bp = (BoughtPrice) Json.deserialize(response.getBody(),BoughtPrice.class);
        boughtprice = decimal.valueOf(boughtPrice);
        ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.Error,'No bought price');
    return boughtprice;


My ordersClass, grabBoughtPrice will run the boughtPriceMock for tests:

 if(test.isRunningTest()) Test.setMock(HttpCalloutMock.class, new 

My Bought price mock:

global class boughtPriceMock implements HttpCalloutMock {
global HTTPResponse respond(HTTPRequest req) {
    HttpResponse res = new HttpResponse();
    res.setHeader('Content-Type', 'application/json');
    res.setBody('{ “bought_Price" : "5000" }');
    return res;


When it is run in my test, the debug log shows:

23:13:55.136 (8786303993)|VARIABLE_ASSIGNMENT|[45]|request|"System.HttpRequest[Endpoint=null, Method=null]”|0x74d0a310

Which fails the test because the boughtPrice is not being brought back from the mock.How can I bring back the boughtPrice in my mock for my tests?

Test class:

Test.setMock(HttpCalloutMock.class, new boughtPriceMock());

  • Is this a REST call out, or a web service callout? They are two different things.
    – sfdcfox
    Sep 16 '18 at 5:55
  • It is a webservice class that invokes the rest get callout grabBoughtPrice in it..
    – j.koh
    Sep 16 '18 at 13:04
  • Was the Orders class generated from a WSDL, though? Does it specifically make use of the code WebServiceCallout.invoke method anywhere in it?
    – sfdcfox
    Sep 16 '18 at 15:13
  • It does not at all. It just sets the variable, bought price ,by setting it from the orders.grabboughtprice
    – j.koh
    Sep 16 '18 at 18:05
  • What does Orders.grabBoughtPrice do, though? Please be as specific as possible.
    – sfdcfox
    Sep 16 '18 at 18:33
HttpResponse response= new HttpResponse();

You're never calling your mock, because you specifically never call http.send when you're running a unit test. Instead, change your code to just:

HttpResponse response = http.send(request);

The point of using a callout mock is to have your live code operate as normal, but intercept the callout with code that you write on your own (the mock).

if(test.isRunningTest()) Test.setMock(HttpCalloutMock.class, new boughtPriceMock());

This code should be (without the if statement), in your unit test method, not in your live code, as mentioned in 朱保田's answer.

As a best practice, do not use Test.isRunningTest unless the documentation specifically states that you must do so to avoid a unit test error.

  • Hi thank you for your input but after making the changes i get this error: "Methods defined as TestMethod do not support Web service callouts" i also updated my post as well.
    – j.koh
    Sep 16 '18 at 23:09
  • You're trying to cover updateBoughtPrice, right? Why are you calling grabBoughtPrice directly, then? Also, you should set the mock after Test.startTest(), but before calling your method.
    – sfdcfox
    Sep 17 '18 at 0:11
  • Ah yes I removed it, the problem was that my test was failing because in my mock my "setBody" was not bringing back the right boughtPrice, I had to increase the boughtPrice so it wasn't coming in null
    – j.koh
    Sep 17 '18 at 0:45

Test.setMock(HttpCalloutMock.class, new boughtPriceMock()); should be written in the test class

  • It is still coming back: VARIABLE_SCOPE_BEGIN|[45]|request|System.HttpRequest|true|false 00:37:33.829 (11289657108)|VARIABLE_ASSIGNMENT|[45]|request|"System.HttpRequest[Endpoint=null, Method=null]"|0x2f1e6998
    – j.koh
    Sep 16 '18 at 5:38

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.