Your question seems to be asking about which of the three examples you have given is the preferred approach.
Example 1 using the Reflection TestUtils is not a good approach for Unit testing. You really don't want to be loading the spring context at all for a unit test. Just mock and inject what is required as shown by your other examples.
You do want to load the spring context if you want to do some Integration testing, however I would prefer using @RunWith(SpringJUnit4ClassRunner.class)
to perform the loading of the context along with @Autowired
if you need access to its' beans explicitly.
Example 2 is a valid approach and the use of @RunWith(MockitoJUnitRunner.class)
will remove the need to specify a @Before method and an explicit call to MockitoAnnotations.initMocks(this);
Example 3 is another valid approach that doesn't use @RunWith(...)
. You haven't instantiated your class under test HelloFacadeImpl
explicitly, but you could have done the same with Example 2.
My suggestion is to use Example 2 for your unit testing as it reduces the code clutter. You can fall back to the more verbose configuration if and when you're forced to do so.