Java – EasyMock and Unitils equivalent to Mockito @ InjectMocks

easymockjavamockingmockitounitils

Is there any techniques available in EasyMock or Unitils Mock (Not Unitils supported EasyMock) to inject the mocks directly into the Class Under Test?

For eg. in Mockito it is possible to inject mocks directly into member variables of a class,

public class TimeTrackerTest {
    @InjectMocks   // Used to create an instance the CUT
    private TimeTrackerBean cut;
    @Mock  // Used to create a Mock instance
    EntityManager em;
    @Before
    public void injectMockEntityManager() {
        MockitoAnnotations.initMocks(this);   // Injects Mocks into CUT
    }
    @Test
    ...
}

Can such things be done with EasyMock or Unitils Mock? In easymock, we need a separate setter method in the CUT to support injection from the tests. Am I right or direction injection is somehow possible?

-Thanks

Best Answer

Maybe this thread has gone dead but yes you can now do this using EasyMock 3.2 with the tags @TestSubject, @Mock and running the test with @RunWith(EasyMockRunner.class). See this well written (not by me!) example:

http://henritremblay.blogspot.ie/2013/07/easymock-32-is-out.html