You need to sign in to do that
Don't have an account?
Alex Yorio
Help with Test Class to Cover Triggers
I have inherited a Test Class that covers two triggers in my org.
When I run the Test Class is the Sandbox Developer Console, I have 77% and 76% code coverage for the triggers.
When I try to deploy the triggers to production, they fail with code coverage of 10% and 7&.
The Test Class uses seealldata=true, and I believe this is why the code coverage fails in production, but I do not know how to update this test to get the necessary coverage.
Here is the Test Class:
@isTest(SeeAllData=true)
private class PulseTriggerTests {
static testMethod void doTests() {
WebServiceCallout.isApexTest = true;
//Insert placement
Unicore_Placement__c placement = new Unicore_Placement__c();
placement.Name = 'TestPlacement';
placement.Pulse_ID__c = '1';
insert placement;
//Insert Account
Account a = new Account();
a.Name = 'TestAccount';
a.Type__c = 'Customer';
a.UNGST_Prop_Code__c = 'TestAccount';
a.Name_on_Sign__c = 'TestAccount';
a.Flag_Prop_Code__c = '1';
a.Brand_Name__c = 'Hilton';
//a.Pulse_Environments_To_Sync__c = 'Staging;Demonstration;Development';
insert a;
//Insert Asset
Asset s = new Asset();
s.Name = 'TestAsset';
s.AccountId = a.Id;
s.Status = 'Installed';
s.Asset_Type__c = 'Business Center';
s.Active__c = true;
//s.Pulse_Environments_To_Sync__c = 'Staging;Demonstration;Development';
insert s;
s.Asset_Type__c = 'Tradeshow Screensaver';
update s;
s.Asset_Type__c = 'CP Business Center';
update s;
s.Asset_Type__c = 'Boarding Pass';
update s;
s.Placement__c = placement.Id;
s.Asset_Type__c = 'Concierge Station';
update s;
s.Asset_Type__c = 'Chalkboard';
update s;
s.Asset_Type__c = 'Information Board';
update s;
s.Active__c = false;
update s;
}
public static testMethod void test_sendPulseNotification()
{
WebServiceCallout.isApexTest = true;
Pulse_Sync_Status__c status = new Pulse_Sync_Status__c();
status.Item_ID__c = '1';
status.Object_Type__c = 'Account';
insert status;
Map<String,String> headers;
WebServiceCallout.sendPulseNotification('', '400-code', 'POST', headers, status.Id, 'Production');
}
// run WebServiceCallout.testMe(); from Execute Anonymous to test
public static testMethod void test_logError()
{
//No existing error
WebServiceCallout.logError('1', 'Error', 'Production');
//Make a status
Pulse_Sync_Status__c status = new Pulse_Sync_Status__c();
status.Item_ID__c = '1';
status.Object_Type__c = 'Account';
insert status;
//Error already exists
WebServiceCallout.logError(status.Id, 'Error 2', 'Production');
}
public static testMethod void test_logSuccess()
{
Pulse_Sync_Status__c status = new Pulse_Sync_Status__c();
status.Item_ID__c = '1';
status.Object_Type__c = 'Account';
status.Last_Message__c = 'Error';
insert status;
WebServiceCallout.logSuccess(status.Id, 'Success', 'Production');
}
public static testMethod void test_logUnregistered()
{
Pulse_Sync_Status__c status = new Pulse_Sync_Status__c();
status.Item_ID__c = '1';
status.Object_Type__c = 'Account';
status.Last_Message__c = 'Error';
insert status;
WebServiceCallout.logUnregistered(status.Id, 'Success', 'Production');
}
}
When I run the Test Class is the Sandbox Developer Console, I have 77% and 76% code coverage for the triggers.
When I try to deploy the triggers to production, they fail with code coverage of 10% and 7&.
The Test Class uses seealldata=true, and I believe this is why the code coverage fails in production, but I do not know how to update this test to get the necessary coverage.
Here is the Test Class:
@isTest(SeeAllData=true)
private class PulseTriggerTests {
static testMethod void doTests() {
WebServiceCallout.isApexTest = true;
//Insert placement
Unicore_Placement__c placement = new Unicore_Placement__c();
placement.Name = 'TestPlacement';
placement.Pulse_ID__c = '1';
insert placement;
//Insert Account
Account a = new Account();
a.Name = 'TestAccount';
a.Type__c = 'Customer';
a.UNGST_Prop_Code__c = 'TestAccount';
a.Name_on_Sign__c = 'TestAccount';
a.Flag_Prop_Code__c = '1';
a.Brand_Name__c = 'Hilton';
//a.Pulse_Environments_To_Sync__c = 'Staging;Demonstration;Development';
insert a;
//Insert Asset
Asset s = new Asset();
s.Name = 'TestAsset';
s.AccountId = a.Id;
s.Status = 'Installed';
s.Asset_Type__c = 'Business Center';
s.Active__c = true;
//s.Pulse_Environments_To_Sync__c = 'Staging;Demonstration;Development';
insert s;
s.Asset_Type__c = 'Tradeshow Screensaver';
update s;
s.Asset_Type__c = 'CP Business Center';
update s;
s.Asset_Type__c = 'Boarding Pass';
update s;
s.Placement__c = placement.Id;
s.Asset_Type__c = 'Concierge Station';
update s;
s.Asset_Type__c = 'Chalkboard';
update s;
s.Asset_Type__c = 'Information Board';
update s;
s.Active__c = false;
update s;
}
public static testMethod void test_sendPulseNotification()
{
WebServiceCallout.isApexTest = true;
Pulse_Sync_Status__c status = new Pulse_Sync_Status__c();
status.Item_ID__c = '1';
status.Object_Type__c = 'Account';
insert status;
Map<String,String> headers;
WebServiceCallout.sendPulseNotification('', '400-code', 'POST', headers, status.Id, 'Production');
}
// run WebServiceCallout.testMe(); from Execute Anonymous to test
public static testMethod void test_logError()
{
//No existing error
WebServiceCallout.logError('1', 'Error', 'Production');
//Make a status
Pulse_Sync_Status__c status = new Pulse_Sync_Status__c();
status.Item_ID__c = '1';
status.Object_Type__c = 'Account';
insert status;
//Error already exists
WebServiceCallout.logError(status.Id, 'Error 2', 'Production');
}
public static testMethod void test_logSuccess()
{
Pulse_Sync_Status__c status = new Pulse_Sync_Status__c();
status.Item_ID__c = '1';
status.Object_Type__c = 'Account';
status.Last_Message__c = 'Error';
insert status;
WebServiceCallout.logSuccess(status.Id, 'Success', 'Production');
}
public static testMethod void test_logUnregistered()
{
Pulse_Sync_Status__c status = new Pulse_Sync_Status__c();
status.Item_ID__c = '1';
status.Object_Type__c = 'Account';
status.Last_Message__c = 'Error';
insert status;
WebServiceCallout.logUnregistered(status.Id, 'Success', 'Production');
}
}
To start with, there are 2 things you need to fix in your test class. First, seeAllData shouldn't be set to true unless absolutely necessary.
Second, for unit testing a WebService class, it's good to have a Mock callout class to cover your functionality.
Here's a link for reference: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts_wsdl2apex_testing.htm
Let me know if you have any other doubts.