testing a service, mocking dependencies

2014-09-16 Thread Konstantine Kougios
Hi, say I got a service A, which has a @Reference private B b; Now I want to write a unit test and mock B, how can I inject the mocked dependency? Only reasonable thing I found so far is to have a static factory method on A, public static A forTesting(B b) { … } Thanks, Kostas

Re: testing a service, mocking dependencies

2014-09-16 Thread David Bosschaert
Why not set the field b in your object manually to the mock B for unit testing? Just assign it to the field. If you insist on having it private you can call 'Field.setAccessible(true)' in your unit test and assign it using reflection... Best regards, David On 16 September 2014 14:39,

Re: testing a service, mocking dependencies

2014-09-16 Thread Konstantine Kougios
Well, that¹s similar to the forTesting factory I mention but with the drawback that the state of A is corrupted until the JVM terminates. By corrupted I mean that my mock will be part of A until either the jvm stops or something else injects a different thing to A.b. If the mock is set this way

Re: testing a service, mocking dependencies

2014-09-16 Thread David Jencks
You don't provide enough information to know what you are doing, and I don't understand what you mean by a unit test. If you are writing a DS component and using Felix DS annotations, note that the @Reference on a field without accessors results in byte code generation of accessor methods.

Re: testing a service, mocking dependencies

2014-09-16 Thread Konstantine Kougios
I want to write a unit test which will run outside of the container. I want to mock dependencies. I don’t mind about lifecycle methods as my service won’t use them (or I don’t need / can mock them). Say for example I am writing A that uses B which downloads a big json file from the internet. I

Re: testing a service, mocking dependencies

2014-09-16 Thread Neil Bartlett
Why not just create a setter method for this dependency and use that from the unit test? I've never understood the desire to inject directly into private fields. I also didn't understand your explanation about polluting state. What does the state matter if this is a genuine unit test?

Re: testing a service, mocking dependencies

2014-09-16 Thread Dawid Loubser
The manner in which you inject dependencies in your unit test should be exactly the same as when you deploy the component to a production environment. If you inject into fields in production, you should inject into fields in your test also. If your fields are private, and you're running DI

Re: testing a service, mocking dependencies

2014-09-16 Thread Konstantine Kougios
Setter is ok, provided the bean is visible via an interface to other bundles. But it can create issues on bigger projects with lots of developers. I.e. A developer can use the setter to change the field on production code with disastrous results. Or even on test code that runs inside the

Re: testing a service, mocking dependencies

2014-09-16 Thread Bram Pouwelse
You could also change from unit test to integration test in case a component depends on other components, when you do that you can register your mocks as a normal service and you don't have to make changes to your code just to be able to test it. Regards, Bram 2014-09-16 17:35 GMT+02:00