You need to sign in to do that
Don't have an account?
Daymon Boswell
First Apex Class Deploy - Need Help With my Code Coverage.
Hi everyone, I am not a developer and am running into issues when deploying my Apex Class into Production. When I deploy it says that my Code Coverage is 0%. Hoping someone here can help me.
Here is my Apex Class:
global class ExternalCalloutHelperV2 {
//@future(callout=true)
public static string post(String endpoint, String jsonBody ) {
System.debug('ExternalCalloutHelper::post');
//System.debug('ExternalCalloutHelper::post is disabled in sandbox.');
//return '';
HttpRequest req = new HttpRequest();
//req.setEndpoint(xxx + endpoint);
req.setEndpoint('xxx'); // FOR TESTING ONLY
req.setMethod('POST');
req.setbody(EncodingUtil.urlEncode(jsonBody, 'UTF-8'));
// Specify the required user name and password to access the endpoint
// As well as the header information
String username = 'voices_api';
String password = 'voiced_dot_com_api_pass';
Blob headerValue = Blob.valueOf(username + ':' + password);
String authorizationHeader = 'BASIC ' +
EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization', authorizationHeader);
// Create a new http object to send the request object
// A response object is generated as a result of the request
Http http = new Http();
HTTPResponse res = http.send(req);
return res.getBody();
}
@future(callout=true)
public static void postContactToServer(id [] contactIds) {
for (Integer i = 0; i<contactIds.size(); i++) {
id contactId = contactIds[i];
Contact contactObj;
Account accountObj;
User ownerObj;
Organization organizationObj = [SELECT id, Name FROM Organization];
contactObj = [SELECT AccountId, Account_Number__c, Fax, Phone, Id, Email, FirstName, Terms_of_Service_Privacy_Policy_Opt_in__c, Marketing_Communications_Opt_in__c,
LastName,MailingCity, MailingCountry, MailingState, MailingStreet, MailingPostalCode, OwnerId, Owner_ID__c, Username__c, CAG_Opt_in__c, Beta_Group_Opt_In__c FROM Contact
where Contact.Id = :contactId];
try {
accountObj = [SELECT Fax, Id, Name, AccountNumber, Phone, BillingCity, BillingCountry, Billing_Instructions__c, BillingState, BillingStreet, BillingPostalCode, OwnerId FROM Account
Where Account.Id = :contactObj.AccountId];
} catch (Exception accountError) {
// accountObj is null
}
try {
ownerObj = [SELECT AccountId,ContactId,Email,FirstName, Name, LastName, Phone, Extension, ProfileId, Title, UserRoleId, Id, Username FROM User
Where User.Id = :contactObj.OwnerId];
} catch (Exception owberError) {
// ownerObj is null
}
Map<String, SObject> postMap = new Map<String, SObject>();
postMap.put('organization', organizationObj);
postMap.put('contact', contactObj);
postMap.put('account', accountObj);
postMap.put('owner', ownerObj);
//System.debug(postMap);
String postBodyJSON = JSON.serialize(postMap);
ExternalCalloutHelper.post('contact_update', postBodyJSON);
}
}
webService static string createWebAccount(Id contactId) {
Contact contactObj;
Account accountObj;
User ownerObj;
//Contact ownerContactObj;
Organization organizationObj = [SELECT id, Name FROM Organization];
contactObj = [SELECT Pending_Server_Update__c, AccountId, Account_Number__c, Fax, Phone, Id, Email, FirstName, Terms_of_Service_Privacy_Policy_Opt_in__c, Marketing_Communications_Opt_in__c,
LastName,MailingCity, MailingCountry, MailingState, MailingStreet, MailingPostalCode, OwnerId, Owner_ID__c, Username__c, CAG_Opt_in__c, Beta_Group_Opt_In__c FROM Contact
where Contact.Id = :contactId];
try {
accountObj = [SELECT Fax, Id, Name, AccountNumber, Phone, BillingCity, BillingCountry, Billing_Instructions__c, BillingState, BillingStreet, BillingPostalCode, OwnerId FROM Account
Where Account.Id = :contactObj.AccountId];
} catch (Exception accountError) {
// accountObj is null
}
try {
ownerObj = [SELECT AccountId,ContactId,Email,FirstName, Name, LastName, Title, Phone, Extension, ProfileId, UserRoleId, Id, Username FROM User
Where User.Id = :contactObj.OwnerId];
} catch (Exception owberError) {
// ownerObj is null
}
Map<String, SObject> postMap = new Map<String, SObject>();
postMap.put('organization', organizationObj);
postMap.put('contact', contactObj);
postMap.put('account', accountObj);
postMap.put('owner', ownerObj);
//System.debug(postMap);
String postBodyJSON = JSON.serialize(postMap);
return ExternalCalloutHelper.post('create_account', postBodyJSON);
}
}
My Test Class is:
@isTest
private class ExternalCalloutHelperV2_Test{
@testSetup
static void setupTestData(){
test.startTest();
Account account_Obj = new Account(Name = 'Name142', BillingStreet = 'PratapNagar', BillingCity = 'Jaipur', BillingState = 'Raj', BillingPostalCode = '302022', BillingCountry = 'India', Phone = '54343-58789');
Insert account_Obj;
Contact contact_Obj = new Contact(LastName = 'LastName149', FirstName = 'First505', MailingStreet = 'PratapNagar', MailingCity = 'Jaipur', MailingState = 'Raj', MailingPostalCode = '302022', MailingCountry = 'India', Phone = '54343-99290', Fax = '54343-58413', Email = 'Email38@test.com', Username__c = 'Usern790', Terms_of_Service_Privacy_Policy_Opt_in__c = false, Marketing_Communications_Opt_in__c = false);
Insert contact_Obj;
test.stopTest();
}
static testMethod void test_post_UseCase1(){
List<Account> account_Obj = [SELECT Name,BillingStreet,BillingCity,BillingState,BillingPostalCode,BillingCountry,Phone from Account];
System.assertEquals(true,account_Obj.size()>0);
List<Contact> contact_Obj = [SELECT LastName,FirstName,MailingStreet,MailingCity,MailingState,MailingPostalCode,MailingCountry,Phone,Fax,Email,Username__c,Terms_of_Service_Privacy_Policy_Opt_in__c,Marketing_Communications_Opt_in__c,CAG_Opt_in__c from Contact];
System.assertEquals(true,contact_Obj.size()>0);
ExternalCalloutHelperV2 obj01 = new ExternalCalloutHelperV2();
ExternalCalloutHelperV2.post('test data','test data');
}
static testMethod void test_postContactToServer_UseCase1(){
List<Account> account_Obj = [SELECT Name,BillingStreet,BillingCity,BillingState,BillingPostalCode,BillingCountry,Phone from Account];
System.assertEquals(true,account_Obj.size()>0);
List<Contact> contact_Obj = [SELECT LastName,FirstName,MailingStreet,MailingCity,MailingState,MailingPostalCode,MailingCountry,Phone,Fax,Email,Username__c,Terms_of_Service_Privacy_Policy_Opt_in__c,Marketing_Communications_Opt_in__c from Contact];
System.assertEquals(true,contact_Obj.size()>0);
ExternalCalloutHelperV2 obj01 = new ExternalCalloutHelperV2();
ExternalCalloutHelperV2.postContactToServer(new List<id>());
}
}
Here is my Apex Class:
global class ExternalCalloutHelperV2 {
//@future(callout=true)
public static string post(String endpoint, String jsonBody ) {
System.debug('ExternalCalloutHelper::post');
//System.debug('ExternalCalloutHelper::post is disabled in sandbox.');
//return '';
HttpRequest req = new HttpRequest();
//req.setEndpoint(xxx + endpoint);
req.setEndpoint('xxx'); // FOR TESTING ONLY
req.setMethod('POST');
req.setbody(EncodingUtil.urlEncode(jsonBody, 'UTF-8'));
// Specify the required user name and password to access the endpoint
// As well as the header information
String username = 'voices_api';
String password = 'voiced_dot_com_api_pass';
Blob headerValue = Blob.valueOf(username + ':' + password);
String authorizationHeader = 'BASIC ' +
EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization', authorizationHeader);
// Create a new http object to send the request object
// A response object is generated as a result of the request
Http http = new Http();
HTTPResponse res = http.send(req);
return res.getBody();
}
@future(callout=true)
public static void postContactToServer(id [] contactIds) {
for (Integer i = 0; i<contactIds.size(); i++) {
id contactId = contactIds[i];
Contact contactObj;
Account accountObj;
User ownerObj;
Organization organizationObj = [SELECT id, Name FROM Organization];
contactObj = [SELECT AccountId, Account_Number__c, Fax, Phone, Id, Email, FirstName, Terms_of_Service_Privacy_Policy_Opt_in__c, Marketing_Communications_Opt_in__c,
LastName,MailingCity, MailingCountry, MailingState, MailingStreet, MailingPostalCode, OwnerId, Owner_ID__c, Username__c, CAG_Opt_in__c, Beta_Group_Opt_In__c FROM Contact
where Contact.Id = :contactId];
try {
accountObj = [SELECT Fax, Id, Name, AccountNumber, Phone, BillingCity, BillingCountry, Billing_Instructions__c, BillingState, BillingStreet, BillingPostalCode, OwnerId FROM Account
Where Account.Id = :contactObj.AccountId];
} catch (Exception accountError) {
// accountObj is null
}
try {
ownerObj = [SELECT AccountId,ContactId,Email,FirstName, Name, LastName, Phone, Extension, ProfileId, Title, UserRoleId, Id, Username FROM User
Where User.Id = :contactObj.OwnerId];
} catch (Exception owberError) {
// ownerObj is null
}
Map<String, SObject> postMap = new Map<String, SObject>();
postMap.put('organization', organizationObj);
postMap.put('contact', contactObj);
postMap.put('account', accountObj);
postMap.put('owner', ownerObj);
//System.debug(postMap);
String postBodyJSON = JSON.serialize(postMap);
ExternalCalloutHelper.post('contact_update', postBodyJSON);
}
}
webService static string createWebAccount(Id contactId) {
Contact contactObj;
Account accountObj;
User ownerObj;
//Contact ownerContactObj;
Organization organizationObj = [SELECT id, Name FROM Organization];
contactObj = [SELECT Pending_Server_Update__c, AccountId, Account_Number__c, Fax, Phone, Id, Email, FirstName, Terms_of_Service_Privacy_Policy_Opt_in__c, Marketing_Communications_Opt_in__c,
LastName,MailingCity, MailingCountry, MailingState, MailingStreet, MailingPostalCode, OwnerId, Owner_ID__c, Username__c, CAG_Opt_in__c, Beta_Group_Opt_In__c FROM Contact
where Contact.Id = :contactId];
try {
accountObj = [SELECT Fax, Id, Name, AccountNumber, Phone, BillingCity, BillingCountry, Billing_Instructions__c, BillingState, BillingStreet, BillingPostalCode, OwnerId FROM Account
Where Account.Id = :contactObj.AccountId];
} catch (Exception accountError) {
// accountObj is null
}
try {
ownerObj = [SELECT AccountId,ContactId,Email,FirstName, Name, LastName, Title, Phone, Extension, ProfileId, UserRoleId, Id, Username FROM User
Where User.Id = :contactObj.OwnerId];
} catch (Exception owberError) {
// ownerObj is null
}
Map<String, SObject> postMap = new Map<String, SObject>();
postMap.put('organization', organizationObj);
postMap.put('contact', contactObj);
postMap.put('account', accountObj);
postMap.put('owner', ownerObj);
//System.debug(postMap);
String postBodyJSON = JSON.serialize(postMap);
return ExternalCalloutHelper.post('create_account', postBodyJSON);
}
}
My Test Class is:
@isTest
private class ExternalCalloutHelperV2_Test{
@testSetup
static void setupTestData(){
test.startTest();
Account account_Obj = new Account(Name = 'Name142', BillingStreet = 'PratapNagar', BillingCity = 'Jaipur', BillingState = 'Raj', BillingPostalCode = '302022', BillingCountry = 'India', Phone = '54343-58789');
Insert account_Obj;
Contact contact_Obj = new Contact(LastName = 'LastName149', FirstName = 'First505', MailingStreet = 'PratapNagar', MailingCity = 'Jaipur', MailingState = 'Raj', MailingPostalCode = '302022', MailingCountry = 'India', Phone = '54343-99290', Fax = '54343-58413', Email = 'Email38@test.com', Username__c = 'Usern790', Terms_of_Service_Privacy_Policy_Opt_in__c = false, Marketing_Communications_Opt_in__c = false);
Insert contact_Obj;
test.stopTest();
}
static testMethod void test_post_UseCase1(){
List<Account> account_Obj = [SELECT Name,BillingStreet,BillingCity,BillingState,BillingPostalCode,BillingCountry,Phone from Account];
System.assertEquals(true,account_Obj.size()>0);
List<Contact> contact_Obj = [SELECT LastName,FirstName,MailingStreet,MailingCity,MailingState,MailingPostalCode,MailingCountry,Phone,Fax,Email,Username__c,Terms_of_Service_Privacy_Policy_Opt_in__c,Marketing_Communications_Opt_in__c,CAG_Opt_in__c from Contact];
System.assertEquals(true,contact_Obj.size()>0);
ExternalCalloutHelperV2 obj01 = new ExternalCalloutHelperV2();
ExternalCalloutHelperV2.post('test data','test data');
}
static testMethod void test_postContactToServer_UseCase1(){
List<Account> account_Obj = [SELECT Name,BillingStreet,BillingCity,BillingState,BillingPostalCode,BillingCountry,Phone from Account];
System.assertEquals(true,account_Obj.size()>0);
List<Contact> contact_Obj = [SELECT LastName,FirstName,MailingStreet,MailingCity,MailingState,MailingPostalCode,MailingCountry,Phone,Fax,Email,Username__c,Terms_of_Service_Privacy_Policy_Opt_in__c,Marketing_Communications_Opt_in__c from Contact];
System.assertEquals(true,contact_Obj.size()>0);
ExternalCalloutHelperV2 obj01 = new ExternalCalloutHelperV2();
ExternalCalloutHelperV2.postContactToServer(new List<id>());
}
}