开发者

Mocking TransactionTemplate from Spring.Net with Rhino mocks

I'm trying to create mock of TransactionTemplate

var tTemplate = MockRepository.GenerateMock<TransactionTemplate>();
var tDelegate = MockRepository.GenerateMock<ITransactionCallback>();
tTemplate.Expect(x => x.Execute(tDelegate)).IgnoreArguments().Throw(new Exception());

on last line i get NullPo开发者_运维百科interException from

at Spring.Transaction.Support.TransactionTemplate.Execute(ITransactionCallback action)

any idea what may be the reason?


The TransactionTemplate class in Spring.Net doesn't have virtual methods so RhinoMocks is unable to override the Execute method when you create the mock.

What this means is that you're not actually stubbing out the Execute method but calling through to the real method. That Execute method calls out to an IPlatformTransactionManager object which you haven't yet supplied and thus the null exception occurs.

Given that the Execute method is part of the ITransactionOperations interface, you may be able to get away with creating a mock ITransactionOperations object and using that in the rest of your test.

Alternatively you could try providing a mock IPlatformTransactionManager to the TransactionTemplate class, and also an ITransactionCallback.DoInTransaction() implemetation for using tDelegate.Stub().Do() syntax.

Something like this:

var transactionManager = MockRepository.GenerateMock<IPlatformTransactionManager>();
var mockDelegate = MockRepository.GenerateMock<ITransactionCallback>();
mockDelegate.Stub(t => t.DoInTransaction(null)).IgnoreArguments().Do(...);
var template = new TransactionTemplate(transactionManager);
template.Execute(mockDelegate);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜