EasyMock and Unitils equivalent to Mockito @InjectMocks

Are there any methods available in EasyMock or Unitils Mock (Not Unitils supported by EasyMock) to inject layouts directly into a class test?

For example, in Mockito, you can inject mocks directly into class member variables,

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 this be done with EasyMock or Unitils Mock? In easymock, we need a separate setter method in CUT to support injection from tests. Am I right or is the direction of the injection somehow possible?

-Thanks

+4
source share
4 answers

I don’t know of any annotations that would allow you to do this with EasyMock, however Spring has ReflectionTestUtils , which allows you to easily do injections for private fields without requiring a setter method. Before switching to Mockito, I found this class invaluable.

+6
source

Unitils has an Inject module for injecting mock objects into test objects. See http://unitils.org/tutorial-inject.html for more details.

For instance:

 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"); } } 
+3
source

This thread may have gone out, but yes, now you can do it with EasyMock 3.2 with the tags @TestSubject, @Mock and run the test with @RunWith (EasyMockRunner.class). See This is a well written (not by me!) Example:

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

+3
source

Below are instructions that you can enter files created using @Mock in your fields.

EasyMockSupport.injectMocks (cut);

Here, the cut is the object on which you want to enter mock. For more information, see the link below http://easymock.org/api/org/easymock/EasyMockSupport.html#injectMocks-java.lang.Object-

0
source

Source: https://habr.com/ru/post/1395008/


All Articles