I have created a class Apex class to get info from and external database and insert the info to the account before save. The method is called by a trigger. Everything is working fine but I am getting an error when creating the test class.

Method does not exist or incorrect signature: void makeCallout() from the type BregV3

How can I fix this? Also this I am still new to Apex and would like to learn the best ways to make my code more efficient and suggestions are would be appreciated.

Apex Class:

public class BregV3 {

    public static void makeCallout(set<id> accountId) {
        String  name;
        String  Shippingcountry;
        String  Shippingcity;
        String  Shippingpostalcode;
        String  Shippingstreet;
        String  Billingcountry;
        String  Billingcity;
        String  Billingpostalcode;
        String  Billingstreet;
        Integer employees;

        //Query current account data
        Account acc = [SELECT Id, Name, shippingStreet, ShippingCity, ShippingPostalCode, ShippingCountry, BillingCity, BillingStreet, BillingPostalCode, BillingCountry, NumberOfEmployees, Sync_Error__c, VAT__c FROM Account WHERE ID= :accountId LIMIT 1];
        System.debug('Organization Number is ' +acc.VAT__c);

        //create the HTTP request
        String requestEndpoint='https://test.com/test/api/test/';
        requestEndPoint+= +acc.VAT__c;
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        HttpResponse response=http.send(request);


            // Map the response to variables
            Map<String,Object> results=(Map<String,Object>)JSON.deserializeUntyped(response.getBody());
            Map<String,Object> mainresults=(Map<String,Object>)(results.get('businessAddress'));
                if (results.get('businessAddress') != Null){

                } else {

            Map<String,Object> nextresults=(Map<String,Object>)(results.get('postalAddress'));
                //Check for null results before mapping
                if (results.get('postalAddress') != Null){
                } else {
                    Billingcountry= '';

                acc.Name                = name;
                acc.NumberOfEmployees   = employees;
                acc.Br_nn_ysund_Sync__c = true;
                acc.Sync_Error__c       = '';
                acc.ShippingCity        = Shippingcity;
                acc.ShippingPostalCode  = Shippingpostalcode;
                acc.ShippingStreet      = Shippingstreet;
                acc.ShippingCountry     = Shippingcountry;
                acc.BillingCity         = Billingcity;
                acc.BillingStreet       = Billingstreet;
                acc.BillingPostalCode   = Billingpostalcode;
                acc.BillingCountry      = Billingcountry;

                // Make the update .
                update acc;

        } else if(response.getStatusCode()==400){

           acc.Sync_Error__c = string.valueOf('Error '+response.getStatusCode() +' Invalid Organization Number');
           acc.Br_nn_ysund_Sync__c = false;
           update acc;



Mock Class:

global class BregCalloutMock implements httpCalloutMock{

    // Implement this interface method
    global HTTPResponse respond(HTTPRequest req) {
        // Optionally, only send a mock response for a specific endpoint
        // and method.
        System.assertEquals('https://test.com/test/api/test/', req.getEndpoint());
        System.assertEquals('GET', req.getMethod());

        // Create a fake response
        HttpResponse res = new HttpResponse();
        res.setHeader('Content-Type', 'application/json');
        return res;

Test Class:

private class BregV3Test {

     @isTest static void testCallout() {
        // Set the mock callout class 
        Test.setMock(HttpCalloutMock.class, new BregCalloutMock());

        //calling method to test
        HttpResponse res = BregV3.makeCallout();

        // Verify response received 
        String contentType = res.getHeader('Content-Type');
        System.assert(contentType == 'application/json');
        String actualValue = res.getBody();
        String expectedValue = '{"example":"test"}';
        System.assertEquals(actualValue, expectedValue);
        System.assertEquals(200, res.getStatusCode());

makeCallout() takes a Set<Id> as parameter:

public static void makeCallout(set<id> accountId) {

Apex is saying that there is no method makeCallout() with an empty parameter list; as Apex does support method overloading that would be a distinct method.

Your unit test will need to build applicable test data (at least Accounts) and provide an appropriate Set<Id> parameter.

| improve this answer | |
  • Thank for your answer. Can you explain how to define the set<Id> parameter in the test class. I have tried to define it and then add it as a parameter in the makecallout() method but get errors. "illegal assigmnet from string to set" "Variable set does not exist" and "variable Id does not exist". – Jemat Dec 15 '19 at 14:22
  • set<Id> accountId = '0012o00002Rqg1WAAR'; HttpResponse res = BregV3.makeCallout(set<id> accountid); – Jemat Dec 15 '19 at 14:22
  • @Jemat You cannot assign a String directly to a Set variable. Please review the Apex Developer Guide's section on Sets to learn how to create them. You don't include type definitions when passing parameters to functions. – David Reed Dec 15 '19 at 15:02
  • I was able to fix the errors now. Thanks! – Jemat Dec 15 '19 at 15: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.