Thursday 16 May 2013

Example of mocking a job execution context in Quartz.net and Moq

I wanted to mock the IJobExecution context for my Quartz job today and wanted to use some of the job parameters, there is a little bit to setup, so here's an example of a starting point:

The Trigger

This is simple enough, create an object that implements ITrigger, I called mine "FakeTrigger", here i've only impelmented the StartTimeUTC



   public class FakeTrigger : ITrigger
    {
        public object Clone()
        {
            throw new NotImplementedException();
        }

        public int CompareTo(ITrigger other)
        {
            throw new NotImplementedException();
        }

        public IScheduleBuilder GetScheduleBuilder()
        {
            throw new NotImplementedException();
        }

        public bool GetMayFireAgain()
        {
            throw new NotImplementedException();
        }

        public DateTimeOffset? GetNextFireTimeUtc()
        {
            throw new NotImplementedException();
        }

        public DateTimeOffset? GetPreviousFireTimeUtc()
        {
            throw new NotImplementedException();
        }

        public DateTimeOffset? GetFireTimeAfter(DateTimeOffset? afterTime)
        {
            throw new NotImplementedException();
        }

        public TriggerKey Key
        {
            get { throw new NotImplementedException(); }
        }

        public JobKey JobKey
        {
            get { throw new NotImplementedException(); }
        }

        public string Description
        {
            get { throw new NotImplementedException(); }
        }

        public string CalendarName
        {
            get { throw new NotImplementedException(); }
        }

        public JobDataMap JobDataMap
        {
            get { throw new NotImplementedException(); }
        }

        public DateTimeOffset? FinalFireTimeUtc
        {
            get { throw new NotImplementedException(); }
        }

        public int MisfireInstruction
        {
            get { throw new NotImplementedException(); }
        }

        public DateTimeOffset? EndTimeUtc
        {
            get { throw new NotImplementedException(); }
        }

        public DateTimeOffset StartTimeUtc
        {
            get { return new DateTimeOffset(DateTime.Now); }
        }

        public int Priority
        {
            get { throw new NotImplementedException(); }
            set { throw new NotImplementedException(); }
        }

        public bool HasMillisecondPrecision
        {
            get { throw new NotImplementedException(); }
        }
    }


Creating the context

This is simply a Moq IJobExecutioncontext

_mockJobExecutionContext = new Mock<IJobExecutionContext>();

Creating the Job Detail

A bit more unknown is the JobDetail object, but this is a new JobDetailImpl object :

_jobdetail = new JobDetailImpl("jobsettings", typeof (IJob));

We can then set up the get of this using Moq:


_mockJobExecutionContext = new Mock<IJobExecutionContext>();
_mockJobExecutionContext.SetupGet(p => p.JobDetail).Returns(_jobdetail);


We can also set up the job details such as description and key etc.


_mockJobExecutionContext.SetupGet(p => p.JobDetail.Description).Returns("jobdescription");
_mockJobExecutionContext.SetupGet(p => p.JobDetail.Key).Returns(new JobKey("jobkey",       
         "jobkeyvalue"));

The Job Data Map

The job data map is the settings that get passed to the job, this is a JobDataMap object but is instanciated with a list of key value pairs :


IDictionary<string, object> keyValuePairs = new Dictionary<string, object>();
keyValuePairs.Add("fromAddress", "fromaddress@email.org");
keyValuePairs.Add("toaddress", "toaddress@email.org");
JobDataMap jobDataMap = new JobDataMap(keyValuePairs);

We can then set the get up of that :

_mockJobExecutionContext.SetupGet(p => p.JobDetail.JobDataMap).Returns(jobDataMap );

Wrapping up 
Finally set up the remining bits such as the trigger

_mockJobExecutionContext.SetupGet(p => p.Trigger).Returns(_trigger);

Then we can test :

job.Execute(_mockJobExecutionContext.Object);

Hurah !