Reputation: 61
I use spring to AutoWired Beans in my project, and also I use mockito for test.
@Service
public class A {
someMethod() { }
}
@Service
public class B {
@Autowired
A a;
someMethod() {
a.someMethod();
}
}
@Service
public class C {
@Autowired
B b;
someMethod() {
b.someMethod();
}
}
Now, I want to test class C, with a real Autowired B class, and an mock A class. How can I do this?
Thanks a lot.
Upvotes: 2
Views: 66
Reputation: 61
In fact, this construction, test C but need A in B; not a good way for unittest. Unittest is for test only one UNIT. At last, i moved A in to B, maybe it's a better way for unittest.
Upvotes: 0
Reputation: 77177
Use constructor injection instead of field injection, and you can simply inject mocks as constructor arguments as you would with any other object.
Upvotes: 1