Steve Fenton

Mocking NHJump In Your Unit Tests

NHJump is an excellent way to jump start your NHibernate implementation, giving you instant repositories and query definitions from the word go. Here is a quick bit of information on how you can go about stubbing and mocking the NHJump repository for the purposes of Unit Testing.

Please note… this is an initial draft and there may be a better way to do this! Also, these examples are in NMock2, which just happens to be what I am using today. You can easily transfer the idea to Rhino or Moq as you see fit.

First of all, these examples use the following mocks…

var mockery = new Mockery();
// This is a mock of the repository
IRepository myMockRepository = mockery.NewMock>();
// This is a mock of an executable query
IExecutableQuery _myMockExecutableQuery = mockery.NewMock>();
// This is a mock of an IValue
NhJump.Persistence.Batching.IValue myMockValue = mockery.NewMock>();

Stub the call on the IRepository (ById, ByTitle, All etc).


Stub the call to PrepareQuery (under the hood)


Stub the call to Enumerate (under the hood).

var myReturnValue = new List { myDomainObject };

You may find you also need to Stub other under-the-hood framework stuff, such as…


And as you often get an IValue back from the repository, you’ll need to mock it like this:


If you have some tips and tricks that make this even easier, please shout and I’ll update the examples.

Written by Steve Fenton on