This is my working code to post an happy birthday picture and message on chatter :).

Global class HappyBirthdayRandom2 implements Schedulable{
  //for test method
  public static String CRON_EXP = '0 0 0 3 9 ? 2022';
  global void execute(SchedulableContext sc) 
       // search by static resource name
       List<StaticResource> Pictures=[Select Name, ContentType, Body From StaticResource where Name like 'Filehappy%'];
       List<FeedItem> posts = new List<FeedItem>();
       List<user> lst=[Select id,name from user where date_of_birth__c =today];
          for(User u:lst)
                //generate a random number [0-5] of resource's name. 
               Integer choice=math.mod(Integer.valueof(Math.random()*100),6);

              Blob Decodedbody=Pictures[choice].body;

                FeedItem post = new FeedItem();
               post.ParentId = u.id;
                post.Type = 'ContentPost';
                // id of user that create post
                post.Body = 'Happy birthday to '+u.name+' !';
                post.ContentData = Decodedbody;
                post.ContentFileName = 'Wishes!.jpg';
             insert posts;

and the test class:

public class TestHappyBirthdayRandom {

Private Static testmethod void TestHappyBirthdayRandom (){
  HappyBirthdayRandom2  objMonthlyMail = new HappyBirthdayRandom2 ();

Profile pfl = [select id from profile where name='Standard User'];
// get today's date
date myDate = date.today();

User testUser = new User(alias = 'u1', email='[email protected]',
        emailencodingkey='UTF-8', lastname='Testing', languagelocalekey='en_US',
        localesidkey='en_US', profileid = pfl.Id,  country='United States', CommunityNickname = 'u1',
        timezonesidkey='America/Los_Angeles', username='[email protected]',date_of_birth__c =myDate);

                FeedItem testpost = new FeedItem();
                testpost.ParentId = null;
                testpost.Type = 'ContentPost';
                testpost.Body = 'test!';


// Schedule the test job

  String jobId = System.schedule('testBasicScheduledApex',HappyBirthdayRandom2.CRON_EXP, new HappyBirthdayRandom2());

// Get the information from the CronTrigger API object

  CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, 
     FROM CronTrigger WHERE id = :jobId];

// Verify the expressions are the same


// Verify the job has not run

  System.assertEquals(0, ct.TimesTriggered);

// Verify the next time the job will run

      System.assertEquals('2022-09-03 00:00:00', 



I am not able to run the code in the for loop of the class.

I don't understand because i have set the date of birth equal today.

Please can you help me with the test class, I'm new to the test.

Thank you


  • 1
    It's not clear on what do you need help with. What lines are not covered? You can see an example for the apex scheduler here salesforce.com/us/developer/docs/apexcode/Content/…
    – PepeFloyd
    May 29, 2013 at 20:40
  • Lines not covered are those after the for in the test class;i don't know how execute them.Thank you for the reply.
    – Enry
    May 30, 2013 at 6:38
  • I'm not clear: what problem are you trying to solve? To just get 75-100% code coverage? Is there are an error you cannot resolve? Or just how to write a test class in general?
    – Mike Chale
    May 30, 2013 at 13:56
  • I have updated my question with the error message.Please, can you take a look?
    – Enry
    May 30, 2013 at 14:34

2 Answers 2


testUser is never inserted into the database, so when you write...

testpost.ParentId = testUser.id;

...this is no different than...

testpost.ParentId = null;

...and thus the corresponding error.

You shouldn't have to create the post in your test class, that defeats the purpose of the test class. Your schedulable job already does that. I believe the correct flow for your test class should be:

1) insert user with birthdate of today

2) run HappyBirthdayRandom2

3) query and assert that the happy birthday post was created.

  • I'm still not able to run the code in the for cycle of the class.I have updated the question.Thank you very much!
    – Enry
    May 30, 2013 at 16:37
  • insert the test user! insert testUser;
    – Phil Rymek
    May 30, 2013 at 17:21
  • 100%!you are very kind!:)
    – Enry
    May 30, 2013 at 19:38

when you insert the user, make sure to have one whose birthday is today so that it'll run?

  • you are right, but it doesn't run the code after the select user again.
    – Enry
    May 30, 2013 at 16:38

You must log in to answer this question.

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