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 while running in-container tests, A state is corrupted.
On 16/09/2014 14:56, "David Bosschaert" <[email protected]> wrote: >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, Konstantine Kougios ><[email protected]> wrote: >> 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 > >--------------------------------------------------------------------- >To unsubscribe, e-mail: [email protected] >For additional commands, e-mail: [email protected] > --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

