jeudi 30 juillet 2015

How to inject a mocked service into a dynamically created class

So I have a @Spy which performs a new object creation, related to the refactoring pattern of -> (Method to Object) however, that newly created instance does not have its dependent services injected in.

The code is obviously not my property to post up on this forum, but in pseudo logic, Ill try to get the message across below:

`@Mock private SpecialistService specialistServiceMock;

@InjectMocks private TestObject testObj = new TestObject();`

although TestObject inherits this service from its Abstract parent class, and any usage directly in testObj the specialistServiceMock is not null and works fine.

Now, when testObj hits the new RefactoredMethodToObject() class, and calls its invoke() method, it needs to have specialistServiceMock to do a few things, but specialistServiceMock is null.

I appreciate this looks to be beyond Mockitos capability, I do have PowerMock at hand too.

So in a nutshell, I need to have the specialistServiceMock mocked for actions needed to be performed in the dynamically created instance of RefactoredMethodToObject().

Any ideas would be welcome, and if you need more information, let me know. Thanks

Aucun commentaire:

Enregistrer un commentaire