使用 Rhino 模拟从 Spring.Net 模拟 TransactionTemplate
我试图
var tTemplate = MockRepository.GenerateMock<TransactionTemplate>();
var tDelegate = MockRepository.GenerateMock<ITransactionCallback>();
tTemplate.Expect(x => x.Execute(tDelegate)).IgnoreArguments().Throw(new Exception());
在最后一行创建 TransactionTemplate 的模拟,但我从任何想法中得到 NullPointerException
at Spring.Transaction.Support.TransactionTemplate.Execute(ITransactionCallback action)
可能是什么原因?
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 NullPointerException from
at Spring.Transaction.Support.TransactionTemplate.Execute(ITransactionCallback action)
any idea what may be the reason?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Spring.Net 中的 TransactionTemplate 类没有虚拟方法,因此当您创建模拟时,RhinoMocks 无法覆盖 Execute 方法。
这意味着您实际上并没有删除 Execute 方法,而是调用真正的方法。该 Execute 方法调用您尚未提供的 IPlatformTransactionManager 对象,因此会发生 null 异常。
鉴于 Execute 方法是 ITransactionOperations 接口的一部分,您也许可以创建一个模拟 ITransactionOperations 对象并在测试的其余部分中使用它。
或者,您可以尝试向 TransactionTemplate 类提供模拟 IPlatformTransactionManager,以及使用 tDelegate.Stub().Do() 语法的 ITransactionCallback.DoInTransaction() 实现。
像这样的东西:
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: