@ChristopherSchneider thanks, that worked. but what if initialisation in happening inside method. That mock value is getting override by the initialisation.
– Atul Kumar
Not without changing at least some of the legacy code. Option 1: inject a factory into A which knows how to create instances of B. The test injects a mock factory which can provide a mock B. Option 2 (ugly): extract b= new B(1,2,3) to a protected method. @Spy the instance being tested, and override that protected method to provide a mock B.
– Andrew S
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.OkRead more
@InjectMocks
. stackoverflow.com/questions/16467685/… – Christopher SchneiderA
which knows how to create instances ofB
. The test injects a mock factory which can provide a mockB
. Option 2 (ugly): extractb= new B(1,2,3)
to a protected method. @Spy the instance being tested, and override that protected method to provide a mockB
. – Andrew S