I am woefully lacking in my Class writing skills. I'm curious if anyone can tell me (or give an example that works) why I cannot seem to get many, if any, IF...TRUE statements to be covered by Code Coverage in my Classes? Screenshot attached. Pretty much ALL of my TRUE results are NOT covered, but most of my FALSEs are. Huh??

Code example with 2 uncovered IF...TRUE sections

UPDATED 1130AM EST: Here is the class in question. This class is probably pathetic by normal standards and it pre-dated me, for which I have simply added code from time to time to "get by". I just want to add enough to get my code to clear. :-(


private class mtinsfhTestClass {

static testMethod void testLead1()            /* ----- Lead Testing Section ----- */

    Lead leadRec = new Lead();

    leadRec.FirstName = 'DSG';
    leadRec.LastName = 'DSGTestClassRecord Lead';
    leadRec.Phone = '4696751677';
    leadRec.Email = 'test1@test.com';
    leadRec.LeadSource = 'FXCT DEMO';
    leadRec.Lead_Source_Detail__c = 'FXCT DEMO';
    leadRec.Status = 'New Lead';
    insert leadRec;

static testMethod void testLead2()     /*------------------------*/

    Lead leadRec = new Lead();

    leadRec.FirstName = 'DSG';
    leadRec.LastName = 'DSGTestClassRecord Lead';
    leadRec.Phone = '4696751677';
    leadRec.Email = 'test2@test.com';

    insert leadRec;

static testMethod void testAcct1111()         /* ----- Account Testing Section ----- */

    Account acctRec = new Account();

    acctRec.Name = 'DSGTestClassRecord Account';
    acctRec.Phone = '4696751677';
    acctRec.pb_RecordType_UTP_OnDemand__c = '4696751677';
    acctRec.IP_Address__c = 'test text only';

    insert acctRec;

static testMethod void testTask11()         /* ----- Task Testing Section -----  (plus Opportunity)  */

    Lead objL = new Lead();
    objL.LastName = 'DSGTestClassRecord Lead';
    objL.Company = 'Test Company';
    objL.Status = 'New Lead';
    objL.Email = 'TestEmail11@test.com';
    insert objL;

    Lead leadRec = [select id from Lead where LastName = 'DSGTestClassRecord Lead' and Status='New Lead' and Email <> null limit 1];

    Task taskRec1 = new Task();
    taskRec1.WhoId = leadRec.Id;
    taskRec1.Activity_Type__c = 'Phone Call';
    taskRec1.Status = 'Complete';
    insert taskRec1;    

    Opportunity o = new Opportunity();
    o.Name = 'DSGTestClassRecord Opportunity';
    o.StageName = 'New';
    o.Sub_Stage__c = 'NewSub';
    o.CloseDate = Date.today();
    insert o;

//        Opportunity oppRec = [select id from Opportunity where Name = 'DSGTestClassRecord Opportunity' Limit 1];
    Opportunity OppRec = [select Id From Opportunity Where id=: o.Id];

    oppRec.StageName = 'Closed Lost';
    oppRec.LeadSource = 'Test 1';
    oppRec.Promotions_Received__c = 'abc; 123; xyz;';
    update oppRec;

    Task taskRec3 = [select Id From Task Where whoId = :leadRec.Id Limit 1];
    delete taskRec3;

    delete oppRec;

static testMethod void testTask12()      /*------------------------*/

    Lead objL = new Lead();
    objL.LastName = 'DSGTestClassRecord Lead';
    objL.Company = 'Test Company';
    objL.Email = 'TestEmail12@test.com';
    insert objL;

    Lead leadRec = [select id from Lead where LastName = 'DSGTestClassRecord Lead' and Email <> null limit 1];

    Task taskRec1 = new Task();

    taskRec1.WhoId = leadRec.Id;
    taskRec1.Activity_Type__c = 'Webinar Attendance';
    taskRec1.Status = 'Complete';
    taskRec1.Event_Date__c = Date.today();
    taskRec1.Attended__c = true;

    insert taskRec1;

    Task taskRec3a = [select Id From Task Where whoId = :leadRec.Id Limit 1];
    taskRec3a.Attended__c = true;
    update taskRec3a;
    taskRec3a.Attended__c = false;
    update taskRec3a;

    Task taskRec3 = [select Id From Task Where whoId = :leadRec.Id Limit 1];
    delete taskRec3;

static testMethod void testTask13()      /*------------------------*/

    Account objL2 = new Account();
    objL2.Name = 'DSGTestClassRecord Account';
    String tempChartTxt = 'Charting';
    objL2.put('pb_SubBegin_' + tempChartTxt + '__c' , Date.today() ); 
    objL2.put('pb_CurrSubBeg_' + tempChartTxt + '__c' , Date.today() ); 
    objL2.put('pb_SubEnd_' + tempChartTxt + '__c' , Date.today() ); 
    objL2.put('pb_PrevSubEnd_' + tempChartTxt + '__c' , Date.today() ); 
    insert objL2;

    Account acctRec = [select id from Account where Name = 'DSGTestClassRecord Account' limit 1];

    Task taskRec11 = new Task();

    taskRec11.WhatId = acctRec.Id;
    taskRec11.Activity_Type__c = 'Webinar Attendance';
    taskRec11.Status = 'Complete';
    taskRec11.Event_Date__c = Date.today();
    taskRec11.Attended__c = true;
    insert taskRec11;

    Task taskRec13 = [select Id From Task Where whatId = :acctRec.Id Limit 1];
    delete taskRec13;


 static testMethod void testOpportunity111()      /*------------------------*/

    Lead leadRec = new Lead();

    leadRec.FirstName = 'DSG';
    leadRec.LastName = 'DSGTestClassRecord Lead';
    leadRec.Phone = '4696751677';
    leadRec.Email = 'test3@test.com';
    leadRec.LeadSource = 'FXCT DEMO';
    leadRec.Lead_Source_Detail__c = 'FXCT DEMO';
    leadRec.Status = 'New Lead';
    insert leadRec;

    Lead leadRec31=[select id from Lead where LastName='DSGTestClassRecord Lead' limit 1];
    Database.LeadConvert lc = new database.LeadConvert(); 

    LeadStatus convertstatus = [select Id, MasterLabel from LeadStatus where IsConverted=true limit 1];
    Database.LeadConvert[] lcArray = new Database.LeadConvert[] {lc}; 
    Database.LeadConvertResult[] results = Database.convertLead(lcArray);

    Opportunity o2 = new Opportunity();
    o2.Name = 'DSGTestClassRecord Opportunity 2';
    o2.StageName = 'New';
    o2.Sub_Stage__c = 'Cancelled';
    o2.CLoseDate = Date.today();
    insert o2;

//        Opportunity OppRec1 = [select Id From Opportunity Where Name = 'DSGTestClassRecord Opportunity 2'];
    Opportunity OppRec1 = [select Id From Opportunity Where id=: o2.Id];
    OppRec1.StageName = 'Closed Won';
    OppRec1.Sub_Stage__c = 'Refund';
    OppRec1.Opportunity_Discount_Approved__c = true;
    OppRec1.put('Charting_Start__c' , Date.today() ); 
    OppRec1.put('Charting_Expiration__c' , Date.today() ); 
    OppRec1.put('Purchase_Amount__c' , 1234.56 ); 
    oppRec1.put('Promotions_Received__c' , 'abc; 123; xyz 2;' );
    oppRec1.put('Promotions_Received_Used__c' , true );
    update OppRec1;

    Integer NumClosedWons = 0;
    oppRec1.Sub_Stage__c = 'Paid';
    oppRec1.Purchase_Amount__c = 123.54;
    update OppRec1;


  • Are you sure your tests have data that should evaluate to true? – Mike Chale Dec 18 '12 at 16:03
  • I know some do not, but others seem to; the widespread use of temp variables confuses matters, though. This would be the logical reason, I assume? – AMM Dec 18 '12 at 16:11
  • I posted the code to the class in question in case anyone is nice enough to give me guidance. To me, it's a pretty daunting task, but maybe not to someone more experienced. :-/ – AMM Dec 18 '12 at 16:31

It looks like your test class doesn't cover all the cases in the trigger. For example, to get line 415 of your trigger to run, you need to create an opportunity in your tests that has a StageName of 'Refund' or 'Cancelled'.

For each path your code will take, you need to account for it in your test class. It is hard to tell you exactly what you need to do to account for this since you haven't posted the entire code for the trigger. Take a look at the end of your test class where you have code that makes changes to OppRec1 and does updates. I think if you add more code like this to cover the different paths you'll get better coverage.

Remember that coverage is not the ultimate goal, though. You want to write tests that check to make sure the code is doing what it should. Your test classes should check the data after the trigger runs and make sure all is as expected using assertions. Here's a blog post I wrote a while back on testing best practices.

  • I've tried altering and/or adding references to StageName being Refund or Cancelled, to no avail. I fear that ultimately I'm just woefully lacking in this area, as all I'm doing at this point is adding code and logic to the class to TRY and mimic needed logic in the Trigger and even when I feel I have it, it shows no difference in coverage. :-( – AMM Dec 18 '12 at 18:33
  • How would YOU code lines 414-416 to be covered? I mean, I have reference (now, again) to StageName being set to BOTH Refund, and later Cancelled... and it does nothing for this. I realize though that this code is inside a FOR loop, so I'm unclear how that affects it.. considering OTHER FALSEs below that point work, I don't get it. :-( (IF you don't mind my asking) :-P – AMM Dec 18 '12 at 18:37
  • 1
    Why don't you add some System.debug statements? E.g. if you're expecting StageName to be refund, put System.debug('Stage Name is: '+currOp.StageName); in line 413. Then you can see the value in the debug logs when you run the test. – George S. Dec 18 '12 at 18:50
  • Don't laugh: Where do I go to actually SEE the debug logs? Yes, I'm that green. – AMM Dec 18 '12 at 18:57
  • (at the very least, some more Logic added has gotten me from 72 to 76% - minor victory - but I'm shooting for 90 now) – AMM Dec 18 '12 at 19:04

Some suggestions for you, the good practice would be to make a util class, where you do all the data handling of creating and updating opportunity records

Here is the quick example of what we used

public class TestUtil {

 public static string GetTime()
    return String.valueOf(DateTime.now().getTime());
   * Get a dummy, non-persisted account with random data. If you change any fields here,
 * make sure you successfully re-run all Apex unit tests, as some are probably checking
 * for specific values.
public static Account getTestAccount() {
    Account A = new Account();
    A.Name =  NamePrefixForTest + ' ' + GetTime();
    A.BillingPostalCode = String.valueOf(MathUtils.rand(11111,99999));
    A.BillingState = 'NY';
    A.BillingStreet = 'Laurel';
    A.AccountNumber = String.valueOf(MathUtils.rand(12345,54321));
    A.BillingCity = 'New Hyde Park';
    A.FOB_Destination__c = true;
    A.Invoice_Preference__c = 'ORDER';
    A.ShippingStreet = 'Laurel';
    A.ShippingCity = 'New Hyde Park';
    A.ShippingState = 'NY';
    A.ShippingPostalCode = String.valueOf(MathUtils.rand(11111,99999));
    A.Contract_End__c = Date.today();
    A.Alpha_Account_Name__c = 'Test ' + GetTime();
    return A;
//get dummy contact
public static Contact getTestContact()
    Contact c = new Contact();
    String rt = [SELECT Id from recordtype where name='customer' and sobjecttype='contact'].Id;
    c.LastName='Doe' + GetTime();
    c.MailingStreet='203 Colorado St ' + GetTime();
    return c;


public static Opportunity getTestOppy() {               
    Account a = getTestAccount();
    insert a;        
    return getTestOppy(a.id, UserInfo.getUserId());
  • This certainly would allow better readability. I'm not ready to even consider this yet, at my experience level. :-/ With the help of this site (which has been immeasurably helpful so far), I will get there one day. Thanks. (sigh) – AMM Dec 18 '12 at 18:35

Once you have designed utility class then call this methods directly to and improve code readability

How to invoke like Account a = TestUtil.getTestAccount();

// creating account those util class will take care of dummy accounts

Now what I can see, in your code first non-covered code is under if-case where opportunity stage name is Opportunity.Stage='Cancelled' - there was no assertion

So If I were you I would have asserted using System.AssertEquals(Opportunity.StageName,'Refund'); at the right place,

I recommend, make the util class, then pull up opportunity, make the required changes on field, update opportunity, account, leads with right field, assert after each change and I believe this will help you covering all the scenarios .

  • I don't see how creating a Util class or asserting will solve the OP question. – George S. Dec 18 '12 at 18:47
  • 1
    Removed link to blog and Google Plus group - this sort of thing goes in your profile, not in answers. Thanks! – metadaddy Jan 29 '13 at 20:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.