Search code examples
javamockingmockitoeasymockunitils

EasyMock and Unitils equivalent to Mockito @ InjectMocks


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


Solution

  • I don't know of any annotations that would let you do this with EasyMock, however, Spring has ReflectionTestUtils which will let you easily do injection to private fields without requiring a setter method. Before I switched to Mockito, I found this class invaluable.