How to override default dependencies with Mockbox and Wirebox

136 views Asked by At

I have a logger service that has a ScopeStorage and a LoggerServiceDAO dependency and in my unit test I need to override these to use mock objects I have created. I am using Wirebox AOP to trigger the logging events so I can not just create a mock object and pass it into the constructor of the CustomerContact object

Here is the mocks I am creating:

scopeStorageMock = mockBox.createMock('system.ScopeStorage').$('get', 111);
loggerServiceDAOMock = mockBox.createMock('system.services.daos.loggerServiceDAO').$('insertLog');

Inside my Wirebox binder I have the following mappings:

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");

Is there a way in my unit test to tell Wirebox that when it gets an instance of the CustomerAspect object to use the two mock objects I created with Mockbox?

1

There are 1 answers

0
Yamaha32088 On

I found the solution although it seems somewhat hacky but it works. Basically what I do is I tell Wirebox to unmap the existing CustomerContactLogger and then create a CustomerContactLoggerMock and inject all the Mocked properties. After that I create a new mapping of the CustomerContactLogger and set the value equal to the mocked object.

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);