If I create a mock in my spring context file using Springockito as described here, how do I mock some behavior for it?
What I’m trying to do:
- ClassA is being tested.
- ClassB is autowired in ClassA.
- ClassB is being mocked with Springockito.
- ClassA needs ClassB to do something in its PostConstruct.
- I need to mock ClassB to do that something, since it can’t and shouldn’t really do it.
Doing this is straight forward without using Springockito (using Mockito straight up), but I need to autowire these beans and use Spring in my tests. Any help is appreciated.
I’m not familiar with Springockito, but it looks interesting for some narrow cases – namely integration testing with mocking just a bit.
Anyway, it looks like for a straightforward use case you extend AbstractJUnit4SpringContextTests, you could also autowire ClassB in your test just like you do in ClassA. Then you could define your expected behavior for ClassB in your setup method.
But I think that you need to set up some behavior for the ClassB bean before you get access to it in your setup method. In that case, you may need another bean to set up ClassB to do the expected behavior. So your testContext.xml would have something like this in it:
The ClassBMockSetter would look something like:
I think that would work, but at that point, isn’t it easier to just hand-code your mock ClassB?