When using Mockito 1.9.x, I used Whitebox
to set the field values ββfor the "injection" mocks. Example below:
@Before public void setUp() { eventHandler = new ProcessEventHandler(); securityService = new SecurityServiceMock(); registrationService = mock(RegistrationService.class); Whitebox.setInternalState(eventHandler, "registrationService", registrationService); Whitebox.setInternalState(eventHandler, "securityService", securityService); }
I really like this approach, but now, when I tried to upgrade to Mockito
2.2.7
, I noticed (more precisely, my IDE noticed and told me several times) that Whitebox was no longer found in Mockito.
I found one alternative that can work as a replacement, and this is org.powermock.reflect.Whitebox
, the problem is that I get another dependency (Powermock) just to use Whitebox. / p>
Powermock
also have a class called Whitebox
, but unfortunately it looks like it cannot be used with Mockito 2.2.x
Are there any good alternatives in Mockito that I can use for manual input now that Whitebox
no longer available?
Decision
I wrote in a comment in response to a post made from @JeffBowman. In short, I decided to copy the WhiteBox code and use it, since it is used in most test cases, and the class has no dependencies on other classes. This was the fastest way to solve this problem.
Note The solution suggested by @bcody is a better alternative if you use spring, it does not contain additional code for your service. I got this information until late :(