Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mock object and Spring annotations

I am using Spring annotations in my code to do the DI. So lets say I have a class class1 that depends on another class class2, I define class1 as below:

@Component
public class class1 {

@Resource
private interface2 object2;

}

class2 is an implementation of interface2.

Now lets say I want to mock class2 and pass it to class1, I dont see any constructor or setter in class1. I think Spring uses reflection to inject object2. How can I mock it? Should I add a setter in class1? Or can I reuse the same way spring is doing it - I mean does spring itself have a mock object framework or something, I was planning to use EasyMock for the mocking.

Thanks

like image 235
Arvind Avatar asked Dec 29 '22 12:12

Arvind


1 Answers

The ReflectionTestUtils class in Spring might be helpful.
It seems to do what you are looking for...at least the injection part :-)

like image 135
cjstehno Avatar answered Jan 12 '23 18:01

cjstehno