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
To check the Service class, we need to have an instance of the Service class created and available as a @Bean so that we can @Autowire it in our test class. We can achieve this configuration using the @TestConfiguration annotation.
if you are writing unit tests a recommend you use @Mock and @InjectMocks . But if you really want test all the flow and need to inject classes, you can @RunWith(SpringJUnit4ClassRunner. class) and @Autowired your classes.
You can reflectively inject dependencies directly into the field using ReflectionTestUtils
, e.g.
ReflectionTestUtils.setField( testInstance, "fieldName", fieldValue );
Some would argue that it's preferable to add a package-visible setter method to the class anyway, used solely by the tests. Alternatively, use autowired constructors, rather than autowired fields, and inject the test dependencies into that.
Although it is possible to set these fields via reflection, doing so will prevent your development tools from finding usages of these fields, and make it harder for you to refactor SomeClassToTest
in the future.
It would be better to add public setters for these fields, and place the @Autowired
annotations on these instead. Not only does this avoid reflection, but it also clarifies the external interface of the class and ensures that your unit test uses just this interface. I see that SomeClassToTest
already implements the SomeOtherClass
interface, and I assume that clients of SomeClassToTest
only use this interface, so there is little danger in making the setters on SomeClassToTest
public.
Better still, use constructor injection and make the fields final. You can still use @Autowired
on the constructor arguments.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With