I have class which uses @PostConstruct to cache some objects.
@Component
public class A {
@Autowired
private B b;
private X x;
@PostConstruct
public void init(){
x = b.doSomething()
}
}
public class C {
@Autowired
private A;
}
public class TestClass {
@Autowired
private A;
@Before
public init() {
expect(b.dosomething()).andReturns(x);
}
@Test
public test1() {
//test
}
}
I have mocked B since it makes an HTTP call to get x. But while running this through unit test, I'm getting following error:
Missing behaviour definition for the preceding method call:
B.dosomething().andXXX()
Everything works fine if I do the same thing inside some normal method of class A. I'm using spring for dependency injection.
I think the reason for this behaviour is, since I'm setting expectation on mock of class B inside my unit test which is invoked 'after' this init method is invoked. How can I test this ?