6

I am trying to test an HttpCallout using mocks and I get the following error :

System.TypeException: Supplied type is not an interface Stack Trace Class.System.Test.setMock: line 57, column 1 Class.ProjectCalloutServiceTest.testBillingCalloutService: line 15, column 1

Here is my test class

@IsTest
private class ProjectCalloutServiceTest {

@IsTest
private static void testBillingCalloutService() {

    Account a = new Account(Name = 'Acme');
    insert a;
    Opportunity o = new Opportunity(Name='Test', StageName='Submitted Project', AccountId=a.Id, Amount=1000, CloseDate=Date.Today());
    insert o;

    List<Id> oppList = new List<Id>();
    oppList.add(o.Id);

    Test.setMock(ProjectCalloutServiceMock.class, new ProjectCalloutServiceMock());
    Test.startTest();
        ProjectCalloutService.postOpportunityToPMS(oppList);
    Test.stopTest();
    // runs callout and check results
    o = [select StageName from Opportunity where id =: o.Id];
    System.assertEquals('Submitted Project', o.StageName);
}
}


@isTest
global class ProjectCalloutServiceMock implements HttpCalloutMock {
global HttpResponse respond(HttpRequest req) {
    // Create a fake response
    HttpResponse res = new HttpResponse();
    res.setHeader('Content-Type', 'application/json');
    res.setBody('{"status":"success"}');
    res.setStatusCode(200);
    return res; 
}
}

I can't find any documentation on this error. What could cause this type of exception?

7

See Testing HTTP Callouts by Implementing the HttpCalloutMock Interface (emphasis added):

For the first argument, pass HttpCalloutMock.class, and for the second argument, pass a new instance of your interface implementation of HttpCalloutMock, as follows:

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

After this point, if an HTTP callout is invoked in test context, the callout is not made and you receive the mock response you specified in the respond method implementation.

| improve this answer | |
  • 2
    Nice find, I actually tend to prefer official documentation when it's available. Do you mind if I tweak it to just quote directly from that document? I think it adds more distinct value that way. :) Basically just: For the first argument, pass HttpCalloutMock.class, and for the second argument, pass a new instance of your interface implementation of HttpCalloutMock, as follows: ... – Adrian Larson Feb 3 '17 at 16:19
11

When you pass a Type to the Test.setMock method, you need to pass the interface being implemented, not the class that implements it.

Joy

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

No Joy

Test.setMock(ProjectCalloutServiceMock.class, new ProjectCalloutServiceMock());
| improve this answer | |
  • 1
    I was 19 seconds late ;) – Raul Feb 3 '17 at 16:13

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.