1

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

    }

}

1 Answer 1

2

That's exactly why. Only methods that are either @isTest methods, or flagged with testMethod, will be executed. So, either of these should work:

@isTest static void myUnitTest() { ...
static testMethod void myUnitTest() { ...

Please note that the order of the keywords is vitally important for compiling properly. Also, your methods may be private, which in the code above, is demonstrated by omitting the access modifier keyword.

5
  • Can you elaborate on why the order is important? I think static void testMethod myUnitTest will also work?
    – Adrian Larson
    Commented Feb 15, 2016 at 18:13
  • 2
    @AdrianLarson That won't compile. testMethod must come before the return type.
    – sfdcfox
    Commented Feb 15, 2016 at 18:18
  • So when I use the testMethod keyword, or the @isTest in front of static void, 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.
    – Matt
    Commented Feb 15, 2016 at 19:19
  • You'll probably want to ask that as a separate question/review existing questions. There is definitely a way to mock callouts and web service calls, but they are two separate things. Make sure you're using the right one.
    – sfdcfox
    Commented Feb 15, 2016 at 21:10
  • Got it. I have another question open here: salesforce.stackexchange.com/questions/110170/… but haven't had luck there either. I've tried mocking both callouts and web service calls to no avail.
    – Matt
    Commented Feb 15, 2016 at 22:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .