In my testcase, I call my two spring bean in my testcase. following is the sample code. the FundService mock is not correct in callbackService.callback() call, I found if I create a new Thread, the mockbean mock is not avaliable in the new Thread.
public class TestTopUpServcie {
@MockBean
private FundService fundservice;
@Autowired
private TopUpService topupService;
@Autowired
private CallbackService. callbackService;
@Test
public void testTopUp(){
// do the mock
Mockito.doReturn(new TopupResponse()).when(fundservice).topUp(any());
// in this method call, the mock(fundService.topUp()) is correct, return the TopUpResponse object.
topupService.topup();
// in this method call, it will create new Thread to do some biz logic, in new Thread, the mock fundservice not work. only return null.
callbackService.callback();
}
}
Reference Jar Version
mockito-core 4.11.0 JDK1.8 Junit4 SpringBoot: 1.5.22.RELEASE
Comment From: wilkinsona
Support for Spring Boot 1.5.x ended in August 2019. Please upgrade to Spring Boot 2.7.x or later. If that does not resolve your problem and you would like us to spend some more time investigating, please spend some time providing a complete yet minimal sample that reproduces the problem. You can share it with us by pushing it to a separate repository on GitHub or by zipping it up and attaching it to this issue.
Comment From: stoneCrystal
Support for Spring Boot 1.5.x ended in August 2019. Please upgrade to Spring Boot 2.7.x or later. If that does not resolve your problem and you would like us to spend some more time investigating, please spend some time providing a complete yet minimal sample that reproduces the problem. You can share it with us by pushing it to a separate repository on GitHub or by zipping it up and attaching it to this issue.
Thanks wilkinsona, but for my case, I cannot upgrade the spring boot version, do you have any other suggestions?
Comment From: wilkinsona
I'm afraid not. Spring Boot 1.5.x is no longer supported either commercially or in open source. Someone on Stack Overflow may be able to help you, but you will have to provide a minimal, reproducible example that makes it as easy as possible for them to reproduce the problem.
Comment From: stoneZ008
Support for Spring Boot 1.5.x ended in August 2019. Please upgrade to Spring Boot 2.7.x or later. If that does not resolve your problem and you would like us to spend some more time investigating, please spend some time providing a complete yet minimal sample that reproduces the problem. You can share it with us by pushing it to a separate repository on GitHub or by zipping it up and attaching it to this issue. demo.zip