You need to sign in to do that
Don't have an account?
msaz87
Why is this test giving 0% coverage?
I have a simple trigger that I'm trying to deploy, but am getting a 0% code coverage error when validating. I had to modify the test slightly to get around another problem (changed "static testMethod void" to "public static void") -- and I'm not sure if that's why the coverage isn't functioning properly or not. Here's my code:
Trigger:
Trigger:
trigger updateContactAfterConverted on Lead (after update) { for(Lead lead:System.Trigger.new) { // was the lead converted? if (Lead.IsConverted) { // query new contact Contact newContact = [SELECT Id FROM Contact WHERE Contact.Id = :lead.ConvertedContactId]; // run @future class to update contact after conversion completed updateContactAfterConvertedFuture.myMethod(newContact.id); } } }Class called in trigger:
public class updateContactAfterConvertedFuture { @future public static void myMethod(String newContact) { // Find new contact Contact updateContact = [SELECT Id FROM Contact WHERE Contact.Id = :newContact LIMIT 1]; // Set field to true updateContact.Conversion_Completed__c = TRUE; // Update contact update updateContact; } }Test:
@isTest public class testUpdateContactAfterConvertedOrig { public static void myUnitTest() { // Create new test lead Lead myLead = new Lead(LastName = 'Fry', Company='Fry And Sons', LeadSource = 'Advertising', Lead_Source_Detail__c = 'PPC'); insert myLead; // Convert test lead Database.LeadConvert lc = new Database.LeadConvert(); lc.setLeadId(myLead.id); // Check conversion LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1]; lc.setConvertedStatus(convertStatus.MasterLabel); // Declare successful Test.startTest(); Database.LeadConvertResult lcr = Database.convertLead(lc); Test.stopTest(); System.assert(lcr.isSuccess()); } }
@isTest
private class testUpdateContactAfterConvertedOrig {
static testmethod void myUnitTest() {
make your class and method private.
trigger updateContactAfterConverted on Lead (after update) {
List<ID> convertedList = new List<ID>();
for(Lead lead:System.Trigger.new) {
// was the lead converted?
if (Lead.IsConverted)
convertedList.add(lead.ConvertedContactId);
}
if(convertedList.size() > 0){
// query new contact
Map<ID, Contact> newContactMap = new Map<ID,Contact>([SELECT Id FROM Contact WHERE Contact.Id IN :convertedList]);
// run @future class to update contact after conversion completed
updateContactAfterConvertedFuture.myMethod(newContactMap.keyset());
}
}
public class updateContactAfterConvertedFuture {
@future
public static void myMethod(List<ID> newContactList) {
List<Contact> updateContactList = new List<Contact>();
for(id v_ID : newContactList){
Contact updateContact = new Contact(ID = v_ID);
// Set field to true
updateContact.Conversion_Completed__c = TRUE;
updateContactList.add(updateContact);
}
// Update contacts
update updateContactList;
}
}
Test coverage should work even without bulkyfied code.
@isTest
private class testUpdateContactAfterConvertedOrig {
static testmethod void myUnitTest() {
Test.startTest();
// Create new test lead
Lead myLead = new Lead(LastName = 'Fry', Company='Fry And Sons', LeadSource = 'Advertising', Lead_Source_Detail__c = 'PPC');
insert myLead;
// Convert test lead
Database.LeadConvert lc = new Database.LeadConvert();
lc.setLeadId(myLead.id);
// Check conversion
LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
lc.setConvertedStatus(convertStatus.MasterLabel);
// Declare successful
//Test.startTest();
Database.LeadConvertResult lcr = Database.convertLead(lc);
Test.stopTest();
System.assert(lcr.isSuccess());
}
Try not to run local test classes while deploying and see if it works.
http://salesforce.stackexchange.com/questions/96283/after-winter-16-upgrade-started-receiving-error-for-test-failures-while-deploym
Runs all tests in prod to see if any are failing their, fix and then deploy new code.
it will be better to fix it. Might be due to winter 16 salesforce update.
To fix prod test class : if failing class is using future with callout(@future(callout=true)), you should implement mock test for it . if not then just fix it by surrounding future call with Test.startTest() and Test.StopTest().
I also tried removing the lead conversion portion from the test class and only uploaded that -- and it passed. But without the conversion part, the trigger won't hit code coverage. I assume this means the problem lies within the conversion? But how can I fix it?