Reputation: 1203
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
Upvotes: 5
Views: 7271
Reputation: 11
The following will help to inject mocks created with @Mock on its fields.
EasyMockSupport.injectMocks(cut);
Here cut is the object on which to inject mocks.For more information refer the below link http://easymock.org/api/org/easymock/EasyMockSupport.html#injectMocks-java.lang.Object-
Upvotes: 1
Reputation: 117
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
Upvotes: 6
Reputation: 16380
Unitils has the "Inject" module for injection of mock objects into tested objects. See http://unitils.org/tutorial-inject.html for details.
For example:
public class MyServiceTest extends UnitilsJUnit4
{
@TestedObject MyService myService;
@InjectIntoByType Mock<MyDao> myDao;
@Test
public void myTestMethod()
{
myDao.returns("something").getSomething();
myService.doService();
myDao.assertInvoked().storeSomething("something");
}
}
Upvotes: 3
Reputation: 5971
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.
Upvotes: 6