1

I'm blanking on why my trigger is indicating 0% test coverage. I feel like I've treated it like my other triggers, but it is no different than it was when I didn't have a test class at all. Any ideas? Below are pasted the trigger and the test class.

Trigger

trigger SetOpportunityPreviousDates on Opportunity (before insert, before update) {

  // create a set of account ids
  Set<ID> setAccountIDs = new Set<ID>();

  // create set of all close dates
  Set<Date> allCloseDates = new Set<Date>();

  Date closeDate = null;
  Date closeDateWithValue = null;

  // store the close dates for the current opportunity, and check to see if it has a value
  for (Opportunity newOpportunity : Trigger.new) {
    if (newOpportunity.CloseDate != null) {
      closeDate = newOpportunity.CloseDate;
      setAccountIDs.add(newOpportunity.AccountId);
    }
    if (newOpportunity.Contribution_value__c > 0) {
      closeDateWithValue = newOpportunity.CloseDate;
    }
  }

  // get list of accounts with one opportunity for each that is earlier than the current close date
  List<Account> accounts = [Select ID, Name, (Select Id, CloseDate, Contribution_value__c From Opportunities WHERE CloseDate < :closeDate ORDER BY CloseDate DESC LIMIT 1) From Account WHERE ID IN :setAccountIDs];

  // set the most recent date and most recent date with value
  Date mostRecentCloseDate = closeDate;
  Date mostRecentCloseDateWithValue = null;
  if (closeDateWithValue != null) {
    mostRecentCloseDateWithValue = closeDateWithValue;
  }

  // for each account matching our trigger
  for(Account account : accounts) {
    // for each account on the opportunity
    for(Opportunity opportunity : account.Opportunities) {
      //System.debug('the matching closedate is ' + opportunity.CloseDate);
      // set the close date
      mostRecentCloseDate = opportunity.CloseDate;
      if (opportunity.Contribution_value__c > 0) {
        // if the value is > 0, set the close date with value
        mostRecentCloseDateWithValue = opportunity.CloseDate;
      }
    }
  }

  // for each opportunity on trigger, set the date fields if they aren't what they should be
  for (Opportunity opportunity : Trigger.new) {
    if (opportunity.Date_of_previous_opportunity__c != mostRecentCloseDate) {
      opportunity.Date_of_previous_opportunity__c = mostRecentCloseDate;
    }
    if (mostRecentCloseDateWithValue != null && opportunity.Date_of_previous_contribution__c != mostRecentCloseDateWithValue) {
      opportunity.Date_of_previous_contribution__c = mostRecentCloseDateWithValue;
    }
  }

}

Test Class

@isTest
private class SetOpportunityPreviousDates_test {

    static void SetOpportunityPreviousDates() {

        // insert some test accounts and contacts
        List<Account> testAccounts = TestDataFactory.createTestAccounts(1, 1, null);

        Id accountId = testAccounts.get(0).Id;

        List<Opportunity> testOpportunities = TestDataFactory.createTestOpportunities(5, accountId);

        List<Opportunity>CountOpportunities = [Select ID from Opportunity];
        System.assertEquals(5,CountOpportunities.size());

        Id campaign = testOpportunities.get(0).CampaignId;

        test.startTest();

        Date today = date.today();
        Opportunity opportunity = new Opportunity(Name = 'test new campaign', AccountId = accountId, Type = 'Donation', Opportunity_Subtype__c = 'Donation: Individual', Recurrence__c = 'One-time', CampaignId = campaign, Payment_Type__c = 'Check', CloseDate = today + 50, StageName = 'Closed Won', Amount = 25);
        System.debug('insert opportunity '+opportunity);


        // create a set of account ids
        Set<ID> setAccountIDs = new Set<ID>();

        // create set of all close dates
        Set<Date> allCloseDates = new Set<Date>();

        Date closeDate = null;
        Date closeDateWithValue = null;

        // store the close dates for the current opportunity, and check to see if it has a value
        if (opportunity.CloseDate != null) {
            closeDate = opportunity.CloseDate;
            setAccountIDs.add(opportunity.AccountId);
        }
        if (opportunity.Contribution_value__c > 0) {
            closeDateWithValue = opportunity.CloseDate;
        }

        // get list of accounts with one opportunity for each that is earlier than the current close date
        List<Account> accounts = [Select ID, Name, (Select Id, CloseDate, Contribution_value__c From Opportunities WHERE CloseDate < :closeDate ORDER BY CloseDate DESC LIMIT 1) From Account WHERE ID IN :setAccountIDs];

        // set the most recent date and most recent date with value
        Date mostRecentCloseDate = closeDate;
        Date mostRecentCloseDateWithValue = null;
        if (closeDateWithValue != null) {
            mostRecentCloseDateWithValue = closeDateWithValue;
        }


        // for each account matching our trigger
        for(Account account : accounts) {
            // for each account on the opportunity
            for(Opportunity accountOpportunity : account.Opportunities) {
                System.debug('the matching closedate is ' + opportunity.CloseDate);
                // set the close date
                mostRecentCloseDate = accountOpportunity.CloseDate;
                if (accountOpportunity.Contribution_value__c > 0) {
                    // if the value is > 0, set the close date with value
                    mostRecentCloseDateWithValue = accountOpportunity.CloseDate;
                }
            }
        }

        // for each opportunity on trigger, set the date fields if they aren't what they should be
        if (opportunity.Date_of_previous_opportunity__c != mostRecentCloseDate) {
            opportunity.Date_of_previous_opportunity__c = mostRecentCloseDate;
        }
        if (mostRecentCloseDateWithValue != null && opportunity.Date_of_previous_contribution__c != mostRecentCloseDateWithValue) {
            opportunity.Date_of_previous_contribution__c = mostRecentCloseDateWithValue;
        }

        insert opportunity;

        test.stopTest();

    }

}
  • Just to eliminate the extremely obvious: did you run the tests from this test class yet? Did they succeed/fail? – pchittum Feb 16 '15 at 15:06
  • @Peter I did run it, but it said 0/0 test methods passed. Maybe I'm missing something obvious to indicate the methods? – Jonathan Stegall Feb 16 '15 at 15:10
5

You forgot to write testmethod.

public static testmethod void SetOpportunityPreviousDates()
{
 // Test Scenarios...
}
  • Thanks so much. Can't imagine how I missed that, but now it's 100%. Sigh. – Jonathan Stegall Feb 16 '15 at 16:01

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.