I am trying to write a Unit test for a class that has several of its fields marked @Autowired
. Given the fact that Spring is automatically resolving the concrete implementations for these fields, I am having a hard time figuring out how to plug my Mock objects(created via EasyMock) as the dependencies during the test-run. Using @Autowired
in the class means lack of setters in that class. Is there a way for me to plug my mock objects in without creating additional setters in class?
Here's an example of what I am trying to accomplish:
public class SomeClassUnderTest implements SomeOtherClass {
@Autowired
private SomeType someType;
@Autowired
private SomeOtherType someOtherType;
@Override
public SomeReturnType someMethodIWouldLikeToTest(){
//Uses someType and someOtherType and returns SomeReturnType
}
}
Here's how I am crafting my Test class before I hit the wall:
public class MyTestClassForSomeClassUnderTest{
private SomeType someType;
private SomeOtherType someOtherType;
@Before
public void testSetUp(){
SomeClassUnderTest someClassToTest = new SomeClassUnderTest();
someType = EasyMock.createMock(SomeType.class);
someOtherType = EasyMock.createMock(SomeOtherType.class);
//How to set dependencies????
}
@Test
public void TestSomeMethodIWouldLikeToTest(){
//??????
}
}
It will be great to get a push in the right direction.
Thanks