I have almost tried all solutions available on internet but i still face the error. my Test class code is
@isTest
private class RegisterTest {
@isTest static void existingUserCheck() {
Registration__c checkInsert = new Registration__c(First_Name__c = 'Nilkamal', Last_Name__c = 'Gotarne', Company__c = 'Xoyal It Services',
Job_Title__c = 'Salesforce Developer', Email__c = '[email protected]', Phone__c = '+917020111572',
Address_Line_1__c = 'Bund garden, pune 411015', Address_Line_2__c = '', City__c = 'Pune',
State__c = 'Maharashtra', Country__c = 'India', Zip__c = '411001');
insert checkInsert;
Register CheckRegister = new Register(new ApexPages.StandardController(new Registration__c()));
CheckRegister.RegData.First_Name__c = 'Nilkamal';
CheckRegister.RegData.Last_Name__c = 'Gotarne';
CheckRegister.RegData.Email__c = '[email protected]';
CheckRegister.RegData.Phone__c = '9021567897';
CheckRegister.RegData.Address_Line_1__c = 'Earth';
CheckRegister.RegData.Address_Line_2__c = 'Mars' ;
CheckRegister.RegData.Company__c = 'Xoya';
CheckRegister.RegData.Job_Title__c = 'Sales Man';
CheckRegister.RegData.Zip__c = '411015';
CheckRegister.RegData.City__c = 'Poona';
CheckRegister.RegData.State__c = 'Maha Rashtra';
CheckRegister.RegData.Country__c = 'Bharat';
Test.startTest();
Test.setMock(HttpCalloutMock.class, new MockHttpResponseRegisterInfo());
CheckRegister.saveUserInfo();
Test.stopTest();
}
}
My class is
public class Register {
public final Registration__c RegData;
public PageReference saveUserInfo() {
if([Select First_Name__c, Last_Name__c, Company__c, Job_Title__c, Email__c, Phone__c, Address_Line_1__c, Address_Line_2__c, City__c, State__c, Country__c, Zip__c, Total_fund__c From Registration__c Limit 1].size() == 0)
{
Registration__c createRegInfo = new Registration__c(First_Name__c = RegData.First_Name__c, Last_Name__c = RegData.Last_Name__c, Email__c = RegData.Email__c,
Phone__c = RegData.Phone__c, Address_Line_1__c = RegData.Address_Line_1__c, Address_Line_2__c = RegData.Address_Line_2__c,
Company__c = RegData.Company__c, Job_Title__c = RegData.Job_Title__c, Zip__c = RegData.Zip__c, City__c = RegData.City__c,
State__c = RegData.State__c, Country__c = RegData.Country__c);
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('http://twil-smsapp.herokuapp.com/api/createRegInfo');
request.setMethod('POST');
request.setTimeout(10000);
request.setHeader('Content-Type', 'application/json;charset=UTF-8');
// Set the body as a JSON object
request.setBody('{"regInfo":'+JSON.serialize(createRegInfo)+',"organisationId":"'+UserInfo.getOrganizationId()+'" ,"createdBy":"'+userInfo.getUserId()+'"}');
//System.debug('{"contactInfo":'+JSON.serialize(contact)+',"message":'+JSON.serialize(mess.Text_Message__c)+',"templateId":"'+template_id+'","createdBy":"'+userInfo.getUserId()+'"}');
HttpResponse response = http.send(request);
System.debug('{"regInfo":'+JSON.serialize(createRegInfo)+',"organisationId":'+UserInfo.getOrganizationId()+' ,"createdBy":"'+userInfo.getUserId()+'"}');
// Parse the JSON response
if (response.getStatusCode() != 200) {
System.debug(response.getBody());
System.debug('The status code returned was not expected: ' +
response.getStatusCode() + ' ' + response.getStatus());
ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Oops, something went wrong!!'));
} else {
insert createRegInfo;
ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Registered Successfully you can also update below information!'));
System.debug(response.getBody());
}
} else {
Registration__c createRegInfo = [Select First_Name__c, Last_Name__c, Company__c, Job_Title__c, Email__c, Phone__c, Address_Line_1__c,
Address_Line_2__c, City__c, State__c, Country__c, Zip__c, Total_fund__c FROM Registration__c LIMIT 1 FOR UPDATE];
createRegInfo.First_Name__c = RegData.First_Name__c;
createRegInfo.Last_Name__c = RegData.Last_Name__c;
createRegInfo.Email__c = RegData.Email__c;
createRegInfo.Phone__c = RegData.Phone__c;
createRegInfo.Address_Line_1__c = RegData.Address_Line_1__c;
createRegInfo.Address_Line_2__c = RegData.Address_Line_2__c;
createRegInfo.Company__c = RegData.Company__c;
createRegInfo.Job_Title__c = RegData.Job_Title__c;
createRegInfo.Zip__c = RegData.Zip__c;
createRegInfo.City__c = RegData.City__c;
createRegInfo.State__c = RegData.State__c;
createRegInfo.Country__c = RegData.Country__c;
update createRegInfo;
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('http://twil-smsapp.herokuapp.com/api/createRegInfo');
request.setMethod('POST');
request.setTimeout(10000);
request.setHeader('Content-Type', 'application/json;charset=UTF-8');
// Set the body as a JSON object
request.setBody('{"regInfo":'+JSON.serialize(createRegInfo)+',"organisationId":"'+UserInfo.getOrganizationId()+'" ,"createdBy":"'+userInfo.getUserId()+'"}');
//System.debug('{"contactInfo":'+JSON.serialize(contact)+',"message":'+JSON.serialize(mess.Text_Message__c)+',"templateId":"'+template_id+'","createdBy":"'+userInfo.getUserId()+'"}');
HttpResponse response = http.send(request);
System.debug('{"regInfo":'+JSON.serialize(createRegInfo)+',"organisationId":'+UserInfo.getOrganizationId()+' ,"createdBy":"'+userInfo.getUserId()+'"}');
// Parse the JSON response
if (response.getStatusCode() != 200) {
System.debug(response.getBody());
System.debug('The status code returned was not expected: ' +
response.getStatusCode() + ' ' + response.getStatus());
ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Oops, something went wrong!!'));
} else {
insert createRegInfo;
ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Registered Successfully!'));
System.debug(response.getBody());
}
}
//System.debug(regInfo);
return null;
}
}
Mock callout class
@isTest
global class MockHttpResponseRegisterInfo 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('http://example.com/example/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.setMock
beforeTest.startTest()