0

i had a post but accidentally deleted my account -hits Head-

the original post contained my question of how do i test createdDate.hour() if i cannot modify the createddate even to test an account creation.

someone led me down a different path, to test using the Test.isRunningTest() method in the trigger to allow the test to enter the main if statement without challenging the created date. but we were unfinished in resolving the problem.

private void sendAlertsPrivate(List<JungoLeadsFrc__JungoLeads__c> lstNewLead, Boolean passTestDate)
{
    List<JungoLeadsFrc__JungoLeads__c> lstLeadToUpdate = new List<JungoLeadsFrc__JungoLeads__c>();
    List<Contact> lstTempContact = new List<Contact>();
    List<Task> lstTaskToInsert = new List<Task>();
    List<Messaging.SingleEmailMessage> lstMail = new List<Messaging.SingleEmailMessage>();
    User addUser = [SELECT Alias,Email,Id FROM User WHERE Name = 'Jason Frangoulis' LIMIT 1];
    for(JungoLeadsFrc__JungoLeads__c objLead: lstNewLead)
    {
        Integer objCreatedDate = objLead.CreatedDate.hour();
        if(passTestDate)
        {
            if(objLead.JungoLeadsFrc__Zillow_Recipient_Email__c != NULL)
            {
                objCreatedDate = 10;
            }
            else
            {
                objCreatedDate = 20;
            }
        }

        if(objLead.JungoLeadsFrc__Zillow_Recipient_Email__c != null)
        {
            lstLeadToUpdate.add(new JungoLeadsFrc__JungoLeads__c(
            Id = objLead.Id, 
            OwnerId = addUser.Id, 
            JungoLeadsFrc__LeadSource__c = 'Zillow',
            JungoLeadsFrc__Stage__c = 'New'));          
        }
        else
        {
            lstLeadToUpdate.add(new JungoLeadsFrc__JungoLeads__c(
            Id = objLead.Id,
            OwnerId = addUser.Id,
            JungoLeadsFrc__LeadSource__c = 'Personal',
            JungoLeadsFrc__Stage__c = 'New'));
        }


        if(objCreatedDate >= 18 || objCreatedDate <= 8 || passTestDate)
        {

            //Task: After Hours ALERT


            //Email
            if(objLead.JungoLeadsFrc__EmailAddress__c != null) 
            {
                //email html

            }

            else
            {
               //Email if No Email Address to SF ADMINS

            }
        }


        else 
        {

            //Task "Call New Lead"


            // Task "NEW LEAD RECIEVED"


            if(objLead.JungoLeadsFrc__EmailAddress__c != null)
            {

                //Email if recieved 9-5pm
            }

            else
            {
               //Email if NO Email Address
            }
    }

    //send email
    if(!lstMail.isEmpty())
    {
        Messaging.sendEmail(lstMail);
    }

    //Insert Tasks
    if(!lstTaskToInsert.isEmpty())
    {
        insert lstTaskToInsert;
    }

    //update Record
    if(!lstLeadToUpdate.isEmpty())
    {
        update lstLeadToUpdate;
    }

} 

and the Trigger:

trigger JungoLeadTrigger on JungoLeadsFrc__JungoLeads__c(after insert){
JungoLeadFollowup objHandler = new JungoLeadFollowup();
if(Trigger.isAfter && Trigger.isInsert){

    if(Test.isRunningTest()) {
        objHandler.leadAlerts(Trigger.New, true);
    } 
    else {
        objHandler.leadAlerts(Trigger.New, false);
    }

}   

i get about 48% code coverage now. creating records with all 4 combinations of having and not null emailAddress and zillow_recipient_email__c being null and not null. so im in need of help.

Test.setCreatedDate:

    @isTest static void sixToNine()
{

    JungoLeadsFrc__JungoLeads__c sixToNine = new JungoLeadsFrc__JungoLeads__c(
    JungoLeadsFrc__EmailAddress__c = '[email protected]');
    insert sixToNine;
    Test.setCreatedDate(sixToNine.Id, DateTime.newInstance(2016, 8, 21, 19, 0, 0));
    Test.startTest();
    JungoLeadsFrc__JungoLeads__c jungo = [SELECT Id, CreatedDate FROM JungoLeadsFrc__JungoLeads__c WHERE JungoLeadsFrc__EmailAddress__c = '[email protected]'];
    System.assertEquals(sixToNine.CreatedDate, DateTime.newInstance(2016, 8, 21, 19, 0, 0));
    Test.stopTest();
}

Test.setCreatedDate does not execute anything in the primary if statement. this is the original 6pm to 8.59am code block.

if(objLead.CreatedDate.hour() >= 18 || objLead.CreatedDate.hour() <= 8)
7
  • If you debug a.Id, say, after Test.startTest() what time does it show?
    – Dan Jones
    Commented Oct 19, 2016 at 14:47
  • Based on your logic looking at hour ranges, you may also want to look into BusinessHours once you get a handle on what you have so far.
    – Adrian Larson
    Commented Oct 19, 2016 at 14:58
  • System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, JungoLeadTrigger: execution of AfterInsert caused by: System.FinalException: Record is read-only
    – Bryan
    Commented Oct 19, 2016 at 15:45
  • @AdrianLarson thats what i actually looked into before i used the time class. i just couldnt figure it out 100% so after i am done with this i will deep dive into how to get buisness hours up and running.
    – Bryan
    Commented Oct 19, 2016 at 15:46
  • If you're doing field updates in your trigger, it should be on a before event, not after.
    – Adrian Larson
    Commented Oct 19, 2016 at 15:58

2 Answers 2

1

I agree with Adrian in the comments that:

Honestly, you should basically never use Test.isRunningTest

However I do have some good news...

the original post contained my question of how do i test createdDate.hour() if i cannot modify the createddate even to test an account creation.

Actually, if you're using Test Classes, you can change the CreatedDate.

@isTest 
private class SetCreatedDateTest {
    static testMethod void testSetCreatedDate() {
        Account a = new Account(name='myAccount');
        insert a;
        Test.setCreatedDate(a.Id, DateTime.newInstance(2012,12,12));
        Test.startTest();
        Account myAccount = [SELECT Id, Name, CreatedDate FROM Account 
                             WHERE Name ='myAccount' limit 1];
        System.assertEquals(myAccount.CreatedDate, DateTime.newInstance(2012,12,12));
        Test.stopTest();
    }
}

Take a look at the Spring '16 Release Docs here.

Long story short, if you orginal question asked how to change the CreatedDate in unit tests, do it this way. Otherwise I'd suggest not going down said different path. This will allow the test to enter the main if statement with changing the created date

7
  • Haha, you (just) beat me to the punch!
    – Adrian Larson
    Commented Oct 19, 2016 at 14:32
  • 1
    @AdrianLarson Sorry about that! More often than not it's the other way around! ;)
    – Dan Jones
    Commented Oct 19, 2016 at 14:34
  • 1
    Tis the nature of things. Nothing to worry about!
    – Adrian Larson
    Commented Oct 19, 2016 at 14:34
  • 1
    this is where my original problem was... i created tests using just that... i will insert in original post.
    – Bryan
    Commented Oct 19, 2016 at 14:36
  • i edited the original question to contain the original IF and the test used.
    – Bryan
    Commented Oct 19, 2016 at 14:41
0

This wasnt a very popular thread, and trouble shooting of Test.setCreatedDate() did not get very far. errors in comments.

Since there are conditions for both with and without having an Zillow email and as well as having an email address. the If cannot check against weather there is an email address.

Integer objCreatedDate = objLead.CreatedDate.hour();
  if(passTestDate)
    {
      if(objLead.JungoLeadsFrc__Zillow_Recipient_Email__c != NULL)
      {
          objCreatedDate = 10;
      }
      else
      {
          objCreatedDate = 20;
      }
    }

change Email field not being null to First Name not being null.

Integer objCreatedDate = objLead.CreatedDate.hour();

if(passTestDate)
    {
        if(objLead.JungoLeadsFrc__FirstName__c  != null)
            {
                objCreatedDate = 20;
            }
            else
            {
                objCreatedDate= 10;
            }
    }

this will penetrate the first if statement by setting objCreatedDate to 20 if there is a first name.

Test Method:

Method 1:

  • New Lead
  • FisrtName
  • EmailAddress

Method 2:

  • New Lead
  • FisrtName
  • Zillow Recipient EmailAddress

Method 3:

  • New Lead
  • No Name
  • Zillow Recipient EmailAddress

Method 4:

  • New Lead
  • No FisrtName
  • EmailAddress

Resulting in:

@isTest 
private class jungoLeadFollowupTest 
{   
    @isTest static void one()
    {
        JungoLeadsFrc__JungoLeads__c one = new JungoLeadsFrc__JungoLeads__c(
        JungoLeadsFrc__FirstName__c = 'bryan',
        JungoLeadsFrc__EmailAddress__c = '[email protected]' );
        insert one;
    }

    @isTest static void two() 
    {       
        JungoLeadsFrc__JungoLeads__c two = new JungoLeadsFrc__JungoLeads__c(
        JungoLeadsFrc__FirstName__c = 'bryan',
        JungoLeadsFrc__Zillow_Recipient_Email__c = '[email protected]');
        insert two;
    }   

    @isTest static void three()
    {
        JungoLeadsFrc__JungoLeads__c three = new JungoLeadsFrc__JungoLeads__c(
        JungoLeadsFrc__Zillow_Recipient_Email__c = '[email protected]');
        insert three;
    }

    @isTest static void four()
    {
        JungoLeadsFrc__JungoLeads__c four = new JungoLeadsFrc__JungoLeads__c(
        JungoLeadsFrc__EmailAddress__c = '[email protected]' );
        insert four;
    }
}

100% code coverage!

thank you all for the help!

You must log in to answer this question.

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