1

I have a apex trigger that I am trying to write a test class on. I am getting 70% on this and was wondering if someone saw what I was missing?

The items not being covered are:

else if(acc.Total_Opportunities_Won__c==0 && acc.Active_Opportunities__c==0) acc.Customer_Type__c = 'Prospect';

as well as

acc.Account_Clearing_Date__c= system.today();

Am I supposed to have a secondary static test in this? When I tried what I thought I needed to do it gave me the same code coverage.

trigger:

trigger AccountTrigger on Account (before update)
  {

  if(checkRecursive.runOnce())
{
list<Account> account_Updatelist= new list<Account>();
for(Account acc : Trigger.new)
  {

     if(acc.Total_Opportunities_Won__c>=1 && acc.Active_Opportunities__c == 0 && Trigger.oldMap.get(acc.id)!=null && Trigger.oldMap.get(acc.id).Active_Opportunities__c>=1)
        acc.Account_Clearing_Date__c= system.today();

     else if(acc.Total_Opportunities_Won__c>=1 && acc.Active_Opportunities__c >= 1)
         acc.Customer_Type__c = 'Current Client';

      else if(acc.Total_Opportunities_Won__c==0 && acc.Active_Opportunities__c==0)
         acc.Customer_Type__c = 'Prospect';



  }


  }

}

Test class:

@isTest
private class testAccountTrigger {

static testMethod void customerTypeTest(){

/*Create Account*/
Account acc1=new Account();
acc1.Name='Test Account1';
acc1.Account_Status__c='Active';
acc1.OMC_Alliance__c=true;
insert acc1;

/*Create Opportunity*/
Opportunity opp1=new Opportunity();
opp1.Name='Test Opportunity1';
opp1.AccountId=acc1.Id;
opp1.closeDate=date.today();
opp1.stageName='Proposal';
opp1.Type='Net New Account - First Deal';
opp1.Project_Type_2__c='Data & Analytics';    
opp1.Call_Center_Country__c='U.S.';
opp1.Televerde_Ownership__c='Televerde U.S.';
insert opp1;

/*Create CDP*/
Campaign_Design_Profile__c cdp1= new Campaign_Design_Profile__c();
cdp1.name='Test CDP1';
cdp1.Campaign_Type__c='Lead Generation';
cdp1.Related_Opportunity__c=opp1.Id;
cdp1.CDP_Status__c='new';
cdp1.Account_Name_Ref__c=acc1.Id;
cdp1.Volume__c=1324;
cdp1.Estimate_Type__c='FTEs';
cdp1.of_Hours__c=7500;
cdp1.Requested_Start_Date__c=date.today();
cdp1.Requested_Completion_Date__c=date.today(); 
insert cdp1;

opp1.StageName='closed won';
update opp1;

cdp1.CDP_Status__c='Completed';
update cdp1;}

static testMethod void customerTypeTest1(){

/*Create Account*/
Account acc2=new Account();
acc2.Name='Test Account2';
acc2.Account_Status__c='Active';
acc2.OMC_Alliance__c=true;
insert acc2;

/*Create Opportunity*/
Opportunity opp2=new Opportunity();
opp2.Name='Test Opportunity2';
opp2.AccountId=acc2.Id;
opp2.closeDate=date.today();
opp2.stageName='Proposal';
opp2.Type='Net New Account - First Deal';
opp2.Project_Type_2__c='Data & Analytics';    
opp2.Call_Center_Country__c='U.S.';
opp2.Televerde_Ownership__c='Televerde U.S.';
insert opp2;

/*Create CDP*/
Campaign_Design_Profile__c cdp2= new Campaign_Design_Profile__c();
cdp2.name='Test CDP2';
cdp2.Campaign_Type__c='Lead Generation';
cdp2.Related_Opportunity__c=opp2.Id;
cdp2.CDP_Status__c='new';
cdp2.Account_Name_Ref__c=acc2.Id;
cdp2.Volume__c=1324;
cdp2.Estimate_Type__c='FTEs';
cdp2.of_Hours__c=7500;
cdp2.Requested_Start_Date__c=date.today();
cdp2.Requested_Completion_Date__c=date.today(); 
insert cdp2;

}
}
2
  • Create another method in your test class, and this time just create the account, and don't assign an Opportunity to it. I think it will cover the else part.
    – Mahmood
    Commented May 3, 2017 at 21:58
  • 2
    Coverage is not the key aim of unit tests. Rather it is to verify application behavior via assertions. If you have zero assertions, you have not written a unit test. You have written a smoke test.
    – Adrian Larson
    Commented May 3, 2017 at 22:42

2 Answers 2

2

Create another testmethod in your test class, this time create the account but don't create an opportunity.

static testMethod void TestAccount ()
{
    /*Create Account*/
    Account acc1=new Account();
    acc1.Name='Test Account1';
    acc1.Account_Status__c='Active';
    acc1.OMC_Alliance__c=true;
    insert acc1;
    // check the behavior of trigger
    System.assertEquals('Prospect', acc1.Customer_Type__c );
}

That should cover the else part of your trigger.

6
  • I added the code that you provided towards the bottom of the test class but it looks like the code coverage is still the same at 70%
    – SoleHack
    Commented May 3, 2017 at 22:05
  • 1
    Figured out the issue. I needed to then after that do a dml update which then made it so it was showing the else. It is now at 90%. Thank you.
    – SoleHack
    Commented May 3, 2017 at 22:18
  • 1
    No assertions? C'mon man!
    – Adrian Larson
    Commented May 3, 2017 at 22:43
  • 1
    Better, but that's still not really verifying the behavior of the trigger. You should assert against Account_Clearing_Date__c and/or Customer_Type__c.
    – Adrian Larson
    Commented May 3, 2017 at 22:49
  • 1
    Good thing there's SFSE to wake you back up before heading home. :D
    – Adrian Larson
    Commented May 3, 2017 at 22:54
1

While not a direct answer, I think it is important to point out that another answer to this question would be "why do any of this"

There is no need for a trigger and thus no need for a test method

  1. Create a rollup summary field on the account to count the number of won opportunities
  2. Make another for the count of active
  3. Use a WFR to set the value of the customer type and Account Clearing Date accordingly based on 1 and 2

PB is also a possibility

2
  • Total Opportunities Won & Active Opportunities are both rollup summaries that use count. I created this as a trigger as when I built this out in PB it wouldn't work. From everything that I read the issue that I was having was that with the rollup count feature it was not triggering a dml save which in turn wouldn't allow for any kind of ischange functionality.
    – SoleHack
    Commented May 5, 2017 at 20:24
  • @JoshuaAnderson - That is not correct. Rollups cause the triggers to fire on the object the data is rolling up to. If you update an oli it will cause trigger to fire on the Opp AND the account if you have a rollup on the account. You most likely had the WFR incorrectly and I do not think IsChanged works on formula fields. I use createddate != null to cause the WFR to fire every time ....Just fyi it does work.
    – Eric
    Commented May 5, 2017 at 21:28

You must log in to answer this question.

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