I have created class and test class. I got an error like this
System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out :Class.Searchaddress.restapi: line 59, column 1 Class.Searchaddress_test.Testsearchaddress: line 20, column 1
but it displays 81%.how to rectify this error? please help me
Class:
global class Searchaddress {
public String accName{get;set;}
public String StNumber{get;set;}
public String Bcity{get;set;}
public String BPostalcode{get;set;}
public String BCountry{get;set;}
public Id accId{get;set;}
string Id;
list<Account>updateAdd=new list<Account>();
public Searchaddress(ApexPages.StandardController controller) {
id=ApexPages.currentPage().getParameters().get('id');
}
public PageReference autosave(){
for(Account updatelist:[SELECT id,BillingAddress,BillingCity,BillingCountry,BillingLatitude,BillingLongitude,BillingPostalCode,BillingState,BillingStreet FROM Account where id=:id]){
updatelist.BillingStreet=StNumber;
updatelist.BillingCity=Bcity;
updatelist.BillingPostalCode=BPostalcode;
updatelist.BillingState='';
updatelist.BillingCountry=BCountry;
updateAdd.add(updatelist);
}
update updateAdd;
PageReference pageRef = new PageReference('/' + ApexPages.currentPage().getParameters().get('id')+'?inline=0');
return pageRef;
}
public class Properties
{
public String city;
public String label;
public String id;
public String postcode;
public String name;
public String citycode;
public String context;
public Double score;
public String type;
}
@RemoteAction
global static list<String> restapi(string accName){
string jsonStr;
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setHeader('Accept','application/JSON');
req.setEndpoint('http://api-adresse.data.gouv.fr/search/?q='+ EncodingUtil.urlEncode(accName,'UTF-8'));//+'&'+'limit'+'='+'10');//
req.setMethod('GET');
HttpResponse res = h.send(req);
system.debug('res)===>'+res);
system.debug('res.getBody()===>'+res.getBody().replace('\n', ','));
List<String> calOut = new List<String>();
jsonStr= res.getBody();
system.debug('res1===>'+res.getBody());
JSONParser parser = JSON.createParser(jsonStr);
List<String> calOut1 = new List<String>();
JSON2Apex parsed = JSON2Apex.parse(res.getBody());
for (JSON2Apex.Features f : parsed.Features)
{
JSON2Apex.Properties p = f.Properties ;
// calOut1.add(p.label+ ', ' + p.context+ ',' + p.city+ ', ' + p.postcode);
calOut1.add(p.label+' '+'FRANCE');
}
return calOut1;
}
}
Test class:
@isTest
public class Searchaddress_test {
static testMethod void Testsearchaddress() {
Test.startTest();
Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());
Account testAccount = new Account(Name='sample demo',Rfleet_DueDate_Period1__c = '4',Hello__c='sdf', Number_Of_Locations__c=5.67, Sales_Rep__c='samplled');
insert testAccount;
testAccount.BillingStreet='chengalpattu';
update testAccount;
Account myTestTrainee = [SELECT id From Account LIMIT 1];
PageReference myVfPage = Page.Searchaddress;
system.test.setCurrentPage(myVfPage);
ApexPages.currentPage().getParameters().put('id', myTestTrainee.id);//Pass Id to page
ApexPAges.StandardController sc = new ApexPages.StandardController(myTestTrainee);
Searchaddress apextestclass=new Searchaddress(sc);
apextestclass.autosave();
String jsonStr ='Base Product';
Searchaddress.restapi(jsonStr);
}
}
Callout class:
public class CalloutClass {
public static HttpResponse getInfoFromExternalService() {
HttpRequest req = new HttpRequest();
req.setEndpoint('http://api-adresse.data.gouv.fr/search/q='+'france');
req.setMethod('GET');
Http h = new Http();
HttpResponse res = h.send(req);
return res;
}
}
HttpResponse res = h.send(req);