You need to sign in to do that
Don't have an account?
Help with Test Class - Still getting 0% Coverage
Hello,
I've been working on trying to create the test class for the below trigger, but I keep getting an error that I have 0% test coverage. Is there a reason that my Test Class is not covering my Trigger? If someone can please help me with this, I would greatly appreciate it - I've been wrestling with it for a few hours to no avail.
Thank you!!
Trigger (it is updating a field on the Opportunity based on a field from a custom child object):
trigger Update_SE_NHC_Assignment_Final on Network_Health_Check__c (before insert, after update) {
//Set the Opportunities to Update
Set<Id> OpportunityforUpdate = new Set<Id>();
//When adding a new Network Health Check or Updating an existing Network Health Check
if(trigger.isInsert || trigger.isUpdate || trigger.isDelete ){
for(Network_Health_Check__c n: [Select Related_Opportunity__r.id, Assigned_SE__r.id from Network_Health_Check__c]) {
OpportunityforUpdate.add(n.Related_Opportunity__r.id);
List<Opportunity> OpportunitiesToUpdate = new List <Opportunity>();
//Update the SE NHC Assignment field on the related Opportunity to match the SE Assigned field on the Network Health Check Object
for(Opportunity o : [Select Id, SE_NHC_Assignment__c from Opportunity where Id IN :OpportunityforUpdate]){
o.SE_NHC_Assignment__c = n.Assigned_SE__r.id;
OpportunitiesToUpdate.add(o);
}
update OpportunitiesToUpdate;
}
}
}
Test Class:
@isTest
Private class Update_SE_NHC_Assignment_Final_TEST_v2 {
private static testMethod void myUnitTest() {
Opportunity thisopp;
List<Opportunity> thisOpps = [SELECT id, Name, SE_NHC_Assignment__c FROM Opportunity WHERE id = '0064000000KxQ9v' ];
if (thisopps.isEmpty()) {
thisopp = new Opportunity();
thisopp.id = '0064000000XxX4x';
} else {
thisopp = thisopps[0];
Account ConsumerAccount = new Account(Name = 'Consumer Account', Type = 'Consumer');
insert ConsumerAccount;
Account PartnerAccount = new Account(Name = 'Partner Account', Type = 'Partner');
insert PartnerAccount;
ConsumerAccount = [Select Id, Name from Account where ID = :ConsumerAccount.ID];
PartnerAccount = [Select Id, Name from Account where ID = :PartnerAccount.ID];
Network_Health_Check__c n = new Network_Health_Check__c(
Name = 'SE Assignment Test',
Primary_Deployment_Location__c = 'Home',
Primary_Deployment_Location_Type__c = 'HQ',
Applications_Used__c = 'VoIP',
Use_case__c = 'Application Analysis & Readiness - Cloud Service and Voice & Video Readiness',
Related_Opportunity__c = thisopp.id,
Assigned_SE__c = '00540000001ZD6G',
Partner_Account__c = PartnerAccount.ID,
Consumer_Account__c = ConsumerAccount.Id);
insert n;
{
try{
update n;
Opportunity o = [select id, SE_NHC_Assignment__c from Opportunity where id = :n.Related_Opportunity__r.id];
} catch (System.DmlException e){
System.debug('we caught a dml exception: ' + e.getDmlMessage(0));
}
}
}
}
}
- Since V24, all testmethods run by default without visibility to any org data. So, your first testmethod Select statement will find nothing. Your else {} block will never execute
- Your testmethod refers to a hard-coded ID - this a) won't be found due to #1 and b) won't exist when you try to run the testmethod during the PROD deployment
- Best practice is for the testmethod to create test SObjects before any queries/DML operations that you are trying to test in your code. Refer to 'Testing Apex' in the Apex Developer's Guide