-1

Getting the error after running the test, and the code coverage for the class is 73%. I would appreciate if you could point me in the right direction.

Class and the test class is below for your reference:

Class

public with sharing class FSWLeadIntegrationController
{
    // This controller class allows for the transmission of new lead account and contact data to the admin application for integration.
    //    
    private static final String NEWACCOUNTID_URL_PARAM = 'newAccountId';
    private static final String NEWCONTACTID_URL_PARAM = 'newContactId';
    private static final String NEWOPPORTUNITYID_URL_PARAM = 'newOpportunityId';
    private static final String NEWSITE_URL_PARAM = 'newSite';

    public Id newAccountId = null;
    public Id newContactId = null;
    public Id newOpportunityId = null;
    public string newSite = null;

    public FSWLeadIntegrationController()
    {  
        System.Debug('FSWLeadIntegrationController: I am here DEFAULT>>');
    }

    public FSWLeadIntegrationController(ApexPages.StandardController controller)
    {  
        newAccountId = System.currentPageReference().getParameters().get(NEWACCOUNTID_URL_PARAM);
        newContactId = System.currentPageReference().getParameters().get(NEWCONTACTID_URL_PARAM);
        newOpportunityId = System.currentPageReference().getParameters().get(NEWOPPORTUNITYID_URL_PARAM);
        newSite = System.currentPageReference().getParameters().get(NEWSITE_URL_PARAM);
        newSite = newSite == null ? 'Fsw' : newSite;

        System.Debug('FSWLeadIntegrationController: I am here 2>>' + newSite);
    }

    public PageReference NewLeadIntegration()
    {
        System.Debug('FSWLeadIntegrationController: I am here NewLeadIntegration>>');
        SendAccountAndContact(newAccountId, newContactId, newSite);

        PageReference redirectPage = null;
        if (newOpportunityId == null)
        { 
            redirectPage = new PageReference('/'+ newAccountId);
        }
        else
        {
            redirectPage = new PageReference('/'+ newOpportunityId);
        }
        redirectPage.setRedirect(true);
        return redirectPage;
    }

    public static void SendAccountAndContact (Id newAccountId, Id newContactId, String newSite)
    {
        if(!LeadHelper.UseLeadIntegration())
        {
          System.Debug('Not using FswLeadIntegration');
          return;
        }
        // Used to provide data to the admin application.
        AdminIntegration admin = DependencyManager.getAdminIntegration();
        // Pass id to admin for integration async
        admin.insertAccountAndContact(newAccountId, newContactId, newSite);
    }
}

Test Class:

@isTest
private class FSWLeadIntegrationControllerTest {
    // This class tests the New Lead Integration Controller.
    //  
    private static string TEST_PAGE_URL = '';     

    public static testMethod void FSWLeadIntegrationControllertest()
    {
        Lead testLead = new Lead();

        FSWLeadIntegrationController fswLeadIntegrationController = new FSWLeadIntegrationController(new ApexPages.StandardController(testLead));
        testLead.Contact_Site__c = 'FSW';

        PageReference fswLeadIntegrationPageRef = Page.FSWLeadIntegration;
        Test.setCurrentPageReference(fswLeadIntegrationPageRef);

        Account testAccount = new Account(Name = 'Test Only');
        insert(testAccount);       
        fswLeadIntegrationController.newAccountId = testAccount.Id; 
        //
        Contact testContact = new Contact(Email = 'aol@aol.com', LastName='Test LastName');
        insert(testContact);
        fswLeadIntegrationController.newContactId = testContact.Id; 
        //
        fswLeadIntegrationController.newOpportunityId = null; 
        //
        PageReference redirectPage = fswLeadIntegrationController.NewLeadIntegration();

        System.assertNotEquals(redirectPage.getUrl().substring(0, TEST_PAGE_URL.length()), TEST_PAGE_URL);
    }

    public static testMethod void SendAccountAndContactTest()
    {
        PageReference fswLeadIntegrationPageRef = Page.FSWLeadIntegration;
        Test.setCurrentPageReference(fswLeadIntegrationPageRef);

        Lead testLead = new Lead();
        testLead.Contact_Site__c = 'FSW';
        Account testAccount = new Account(Name = 'Test Only');
        insert(testAccount);       
        //
        Contact testContact = new Contact(Email = 'aol@aol.com', LastName='Test LastName');
        insert(testContact);
         //
        FSWLeadIntegrationController.SendAccountAndContact(testAccount.Id, testContact.Id, testLead.Contact_Site__c);
    }
}
  • Did you happen to do a search for the error text? There is a lot of information out there. If you do not understand it maybe you can be more specific with your question – Eric Jun 28 '15 at 22:58
2

You need to use a CalloutMock class that pretends to make the callout.

Here is an example of one:

    global with sharing class UploadFileMock implements HTTPCalloutMock{
    global HTTPResponse respond(HTTPRequest req){
        HttpResponse res = new HTTPResponse();
        res.setHeader('Content-Type', 'application/JSON');
        res.setBody('Your body');
        res.setStatusCode(201);
        return res;
    }
}

Then in your calling code, invoke it like this:

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

You don't need to change any of your other code - the Mock will handle your callout automatically and return the response that you have defined.

It doesn't matter when you call this, as long as you call it before you make the HTTP request.

  • 1
    Small correction: Test.setMock(UploadFileMock.class, new UploadFileMock()); should be Test.setMock(HTTPCalloutMock.class, new UploadFileMock()); Otherwise you will get an error like"Supplied type is not an interface" – Guy Clairbois Jul 14 '16 at 13:37

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