我有一个具有ScopeStorage和LoggerServiceDAO依赖项的记录器服务,在我的单元测试中,我需要覆盖它们才能使用我创建的模拟对象。我使用Wirebox AOP来触发日志记录事件,因此我不能只创建一个模拟对象并将其传递到CustomerContact对象的构造函数中
下面是我正在创建的mock:
scopeStorageMock = mockBox.createMock('system.ScopeStorage').$('get', 111);
loggerServiceDAOMock = mockBox.createMock('system.services.daos.loggerServiceDAO').$('insertLog');在我的Wirebox绑定器中,我有以下映射:
map('CustomerContact').to('models.Customer.CustomerContactBean');
map('LoggerServiceDAO').to('system.Services.DAOs.LoggerServiceDAO');
map('ScopeStorage').to('system.ScopeStorage');
map('CustomerContactLogger').to('models.customer.loggers.CustomerContactLogger');
mapAspect("CustomerAspect").to('models.CustomerAspect');
bindAspect(classes=match().mappings("CustomerContact"), methods=match().methods(['create','delete', 'update']), aspects="CustomerContactLogger");在我的单元测试中,有没有办法告诉Wirebox,当它获得CustomerAspect对象的实例时,可以使用我用Mockbox创建的两个模拟对象?
发布于 2017-11-16 09:01:22
我找到了解决方案,尽管它看起来有点老生常谈,但它是有效的。基本上,我所做的就是告诉Wirebox取消现有CustomerContactLogger的映射,然后创建一个CustomerContactLoggerMock并注入所有模拟的属性。之后,我创建了CustomerContactLogger的新映射,并将该值设置为与模拟对象相等。
injector.getBinder().unMap('customercontactLogger');
customerContactLoggerMock = mockBox.createMock('models.customer.loggers.CustomerContactLogger');
customerContactLoggerMock.$property(propertyName='scopeStorage', mock=scopeStorageMock).$property(propertyName='loggerServiceDAO', mock=loggerServiceDAOMock);injector.getBinder().map('CustomerContactLogger').toValue(customerContactLoggerMock);
injector.getBinder().map('CustomerContactLogger').toValue(customerContactLoggerMock);https://stackoverflow.com/questions/47319140
复制相似问题