0

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 {

    @future(callout=true)
    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();
        request.setEndpoint(requestEndPoint);
        request.setMethod('GET');
        HttpResponse response=http.send(request);

        if(response.getStatusCode()==200){

            // Map the response to variables
            Map<String,Object> results=(Map<String,Object>)JSON.deserializeUntyped(response.getBody());
                name=String.valueOf(results.get('name'));
                employees=Integer.valueOf(results.get('numberOfEmployees'));
            Map<String,Object> mainresults=(Map<String,Object>)(results.get('businessAddress'));
                if (results.get('businessAddress') != Null){
                    Shippingcountry=String.valueOf(mainresults.get('country'));
                    Shippingcity=String.valueOf(mainresults.get('postCode'));
                    Shippingpostalcode=String.valueOf(mainresults.get('streetNumber'));
                    Shippingstreet=String.valueOf(mainresults.get('address')).removeStart('(').removeEnd(')');

                } else {
                    Shippingcountry='';
                    Shippingcity='';
                    Shippingpostalcode='';
                    Shippingstreet='';
                }


            Map<String,Object> nextresults=(Map<String,Object>)(results.get('postalAddress'));
                //Check for null results before mapping
                if (results.get('postalAddress') != Null){
                    Billingcountry=String.valueOf(nextresults.get('country'));
                    Billingcity=String.valueOf(nextresults.get('postCode'));
                    Billingpostalcode=String.valueOf(nextresults.get('streetNumber'));
                    Billingstreet=String.valueOf(nextresults.get('address')).removeStart('(').removeEnd(')');
                } else {
                    Billingcountry= '';
                    Billingcity='';
                    Billingpostalcode='';
                    Billingstreet='';
                }



                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;
                system.debug(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;
           system.debug(response.getStatusCode()); 
           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');
        res.setBody('{"example":"test"}');
        res.setStatusCode(200);
        return res;
    }
}

Test Class:

@isTest
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());
    }
}

1 Answer 1

1

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.

4
  • 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".
    – Jordan
    Commented Dec 15, 2019 at 14:22
  • set<Id> accountId = '0012o00002Rqg1WAAR'; HttpResponse res = BregV3.makeCallout(set<id> accountid);
    – Jordan
    Commented Dec 15, 2019 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
    Commented Dec 15, 2019 at 15:02
  • I was able to fix the errors now. Thanks!
    – Jordan
    Commented Dec 15, 2019 at 15:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .