function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
msaz87msaz87 

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 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());
    
        }
        
    }

 
Vishwajeet kumarVishwajeet kumar
Try to use 'testMethod' keyword, in your test method signature. it should fix it.
msaz87msaz87
So when I use the testMethod keyword, I get 100% code coverage, but I get this error: 
Methods defined as TestMethod do not support Web service callouts 
Stack Trace: null
That's why I removed it to try and get around the error. I've tried mocking a Web Service callout and an HTTP callout and nothing works.
Vishwajeet kumarVishwajeet kumar
This works:

@isTest
private class testUpdateContactAfterConvertedOrig {
    static testmethod void myUnitTest() { 


make your class and method private.
Vishwajeet kumarVishwajeet kumar
Also, you should try to bulkify your code:

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;     
      
       }
    
    }
msaz87msaz87
I updated the class the way you noted and I still get the same error. I will try using your bulkified code and see if that makes a difference.
Vishwajeet kumarVishwajeet kumar
Making class and method private works for me. i just created those classes and trigger in my sandbox and ran test, and it worked without any error.
Test coverage should work even without bulkyfied code.
msaz87msaz87
Everything seems to work fine in my sandbox as well, it's when I try to deploy it to Production that I get the error:
testUpdateContactAfterConvertedOrig
myUnitTest
Methods defined as TestMethod do not support Web service callouts 
Stack Trace: null

 
Vishwajeet kumarVishwajeet kumar
Try :

@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());
    
        }

 
msaz87msaz87
Same result, unfortunately.
testUpdateContactAfterConvertedOrig
myUnitTest
Methods defined as TestMethod do not support Web service callouts 
Stack Trace: null

 
Vishwajeet kumarVishwajeet kumar
Well, then it's not due to this test class, Might be due to some existing test classes in prod.
Try not to run local test classes while deploying and see if it works.

 
Vishwajeet kumarVishwajeet kumar
Deployment changed in winter 15' introducing testLevel, make sure you are setting it to NoTestRun, They might have changed the default level as in winter 15' it was set to NoTestRun but this could now have changed. Check your deployment and validation routines.

http://salesforce.stackexchange.com/questions/96283/after-winter-16-upgrade-started-receiving-error-for-test-failures-while-deploym
 
msaz87msaz87
I've tried only running the testUpdateContactAfterConvertedOrig class during deployment and I get the same result. I get the same error and it also tells me that I get 0% code coverage on updateContactAfterConvertedFuture
Vishwajeet kumarVishwajeet kumar
OK, then you need to check your prod environment. i am pretty sure it's not due to new test class.
Runs all tests in prod to see if any are failing their, fix and then deploy new code.
msaz87msaz87
I tried running the body portion of the test in the developer console in production to view the log and see where the callout is occurring, but I'm not really sure what I'm looking for. The only reference to callouts I found was on LIMIT_USAGE_FOR_NS events, but each time it was Number of callouts: 0 out of 100
Vishwajeet kumarVishwajeet kumar
Well, you should run all test classes, which already exist in production(not new class, which needs to be deployed), to see if any one is failing with error: "Methods defined as TestMethod do not support Web service callouts" and fix those test classes.Then depoy new Test class, once those are fixed.

 
msaz87msaz87
I found another test class (already in production) that's getting the same error -- but why would this class cause an error when I specify during deployment to only run my new test class?
Vishwajeet kumarVishwajeet kumar
Well, i think you should just select default while deploying, if you are using change set. 
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(). 
msaz87msaz87
To test if the @future is the problem, I commented out the class that's using @future from the trigger entirely and am not uploading the class either. But even without that, I still get the same error, which makes me think it's not the @future.

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?
Adrienne MyersAdrienne Myers
I'm having the same issues!  
Vishwajeet kumarVishwajeet kumar
@matt..their could some difference b/w Lead Prcoess in Sandbox and production, which is causing this issue.
msaz87msaz87
Any ideas how or where to check for a difference?
Vishwajeet kumarVishwajeet kumar
Setup=>quick find window=> search for 'Lead Processes'. Verify it for all lead record types, to match prod and sandbox. 
msaz87msaz87
There's no Lead Processes set up for either prod or sandbox. Is there a way to check for what might be making callouts during lead conversion in production?
Vishwajeet kumarVishwajeet kumar
Well setup debug logs and try to convert a lead manually and check debug logs.
msaz87msaz87
Just tried in both production and sandbox and saw no CALLOUT_REQUEST or CALLOUT_RESPONSE entries in either. My debug level had callouts set to INFO