All, need help with test class on Batch apex. Below is batch apex code which make 2 callouts (for Token and actual data) however i`m not able to get test coverege on this class. All I have is 7% coverage. Can someone please help?
global class CalloutsAccounts implements Database.Batchable<sObject>,Database.AllowsCallouts{
global Database.QueryLocator start(Database.BatchableContext BC){
String query = 'SELECT Id FROM Account LIMIT 1';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Account> scope){
HttpRequest obj = new HttpRequest();
HttpResponse res = new HttpResponse();
Http http = new Http();
String reqBody = '{ "user": "Username", "passwd": "user_password" }';
obj.setMethod('POST');
obj.setHeader('Content-Type','application/json');
obj.setEndPoint('https://test.samplepoint.com/api/UserSrvs.svc/Login');
obj.setBody(reqBody);
obj.getheader('Auth-Token');
res = http.send(obj);
authtoken objAuthenticationInfo = (authtoken)JSON.deserialize(res.getbody(), authtoken.class);
String token = res.getHeader('Auth-Token');
Http h1 = new Http();
HttpRequest req1 = new HttpRequest();
String reqBody2 = ' {"Accountype" : "workforce"}';
req1.setHeader('Auth-Token', token);
req1.setHeader('Content-Type','application/json');
req1.setMethod('POST');
req1.setBody(reqBody2);
req1.setEndpoint('https://test.samplepoint.com/api/accservices.svc/accountfeed');
HttpResponse res1 = h1.send(req1);
JSONParser parser = JSON.createParser(res1.getBody());
parser.nextToken();
parser.nextValue();
String fieldName = parser.getCurrentName();
String fieldValue = parser.getText();
accParser deserializeResults3 = new accParser ();
deserializeResults3 = (accParser)System.JSON.deserialize(fieldValue, accParser.class);
List < accParser.cls_account> advisorList = new List< accParser.cls_account>();
advisorList = deserializeResults3.root.accounts.account;
Map <Decimal,Id> AdvisorMap = New Map <Decimal,Id> ();
List <Account> advisorAccList = [SELECT Id, Fact_ID__c, RecordTypeID FROM Account WHERE RecordTypeid = '0124100000091Ho'];
For (Account Acs : advisorAccList) {
If (Acs.Fact_ID__c != null)
AdvisorMap.put(Acs.Fact_ID__c, Acs.ID);
}
Map <String,Id> HouseholdMap = New Map <String,Id> ();
List <Account> advisorAccList1 = [SELECT Id, SSCN__c, RecordTypeID FROM Account WHERE RecordTypeid = '012410000009Il6'];
For (Account Acs1 : advisorAccList1) {
If (Acs1.SSCN__c != null)
HouseholdMap.put(Acs1.SSCN__c, Acs1.ID);
system.debug('@@@'+HouseholdMap);
}
List<Financial_Account__c> lstAccount = new List<Financial_Account__c>();
for(accountParser.cls_account cand : advisorList){
Financial_Account__c PFA = New Financial_Account__c();
//PFA.Cirrus_Unique_ID__c = cand.account_id;
//Map Advisor Lookup
PFA.Advisor_ID__c = cand.advisor_id;
PFA.Advisor__c = AdvisorMap.get(PFA.Advisor_ID__c );
//Map Household Client Lookup
PFA.Household_ID2__c = cand.household_id;
if (HouseholdMap.get(PFA.Household_ID2__c) == null) {
PFA.Client__c = '0013C000003Ywet';
} else if (PFA.Household_ID2__c != null) {
PFA.Client__c = HouseholdMap.get(PFA.Household_ID2__c);
}
PFA.Unique_ID__c = cand.account_id;
PFA.Financial_Account_Number__c = cand.accountnum;
PFA.Account_Type__c = cand.account;
PFA.Tax_Status__c= cand.taxablestatus;
PFA.Investment_Objective__c = cand.objective;
if (cand.inception_date != null) {
PFA.Account_Opening__c = date.parse(cand.inception);
}
if (cand.perf_begin_date != null) {
PFA.perf_begin_date__c = date.parse(cand.perfdate);
}
// if (cand.household_id != null) {
// PFA.Household_ID__c = cand.housedate_id;
// }
PFA.Account_Type__c = cand.account_type;
// PFA.Client__c = '0013C000003CRTr'; LOOKUP TO ACCOUNTS
PFA.compute_flg__c = cand.compute_flg;
PFA.Account_Description__c = cand.description;
PFA.fwc_hh_id__c = cand.fwc_hh_id;
if (cand.termination_date != null) {
PFA.termination_date__C = date.parse(cand.termination_date);
}
if (cand.last_date != null) {
PFA.last_date__c = date.parse(cand.last_date);
}
PFA.Custodian_Id__c = cand.custodian_id;
PFA.billing_account_num__c = cand.billing_account_num;
PFA.rebal_method__c = cand.rebal_method;
lstAccount.add(PFA);
}
Boolean isUpsertfirstTime = true;
try {
upsert lstAccount Financial_Account_Number__c;
}catch (DMLException e) {
System.debug('Re-trying');
if(isUpsertfirstTime){
upsert lstAccount Financial_Account_Number__c;
isUpsertfirstTime = false;
}
}
}
global void finish(Database.BatchableContext BC){
CalloutsAccounts2 myaccBatch = new CalloutsAccounts2();
Id batchProcessId = Database.executeBatch(myacctBatch);
}
}
Mock:
@isTest
global class MockHttpResponseGenerator 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.samplepoint.com/api/UserSrvs.svc/', req.getEndpoint());
System.assertEquals('POST', req.getMethod());
// Create a fake response
HttpResponse res = new HttpResponse();
res.setHeader('Content-Type', 'application/json');
res.setBody(''{ "user": "Username", "passwd": "user_password" }');
res.setHeader('Auth-Token', '3+yV8B+7iSTu7Oj4alK4/fJPY1a5VRhAre6jG5vx6kDTXMOENFWJqAIQpuYE8nOdLwDmQBdo=');
res.setStatusCode(200);
return res;
}
}
TEst Class:
@isTest
private class CalloutClassTest {
@isTest static void testCallout() {
// Set mock callout class
Test.setMock(HttpCalloutMock.class, new MockHttpResponseGenerator());
Test.startTest();
Database.executeBatch(new CalloutsAccounts(), 100);
Test.stopTest();
}