Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit Testing quartz-dot-net (Quartz.Net) Execute function

Tags:

c#

quartz.net

I would like to test the Execution function of my Quartz.Net jobs, which implement the Quartz.IJob:

public void Execute(IJobExecutionContext context)

The problem: how to create a new IJobExecutionContext class instance. Or even instead of creating a New one, just get it from an already existing scheduled job.

The goal is to create a test such as:

[Test()]
public void Test()
{
    IJobExecutionContext jobExecutionContext = ???; //get a defined JobExecutionContext somehow...
    m_myJob.Execute(jobExecutionContext);
}

All I have found has been this related post with the same problem. All he said was "Solved by passing only JobDataMap object to my method: context.MergedJobDataMap" which I don't seem to be able to reproduce. And the other proposition, the use of Moq, is out of reach for me at this current time. Although if it really is the only way, I will focus on this solution. But I thought it was reasonable if I tried to find a simpler way first.

like image 937
Xavier Peña Avatar asked Sep 16 '25 01:09

Xavier Peña


1 Answers

IJobExecutionContext is an interface, which means you can create a mock version of it easily. Either by using a Mocking framework, like Moq as you mentioned, or by creating a class just for use in your test

private class MockExecutionContext : IJobExecutionContext 
{


}

you will need to implement all the members of the interface, and give them the behaviour which your test needs, but the compiler/intellisense will help you with this. Depending on what you are testing, you should be able to give most of the members 'stub' implementations that do nothing other than satisfy the member signatures.

like image 151
Jonny Cundall Avatar answered Sep 19 '25 11:09

Jonny Cundall