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