Search code examples
spring-bootasynchronousjunitretry-logic

Asyn retry Junit is not running for 3 times as default


I am working on junit test for failure scenario for retryable. It used to work fine until we have upgraded to new version of mockito and spring boot version. We have updated to spring boot 2.4.x and started seeing this issue.

Service.java

public class RetryTest 
{
    @Autowired
    private RetryTemplate retryTemplate;

    @Async
     private void retryTest(String in) {
          retryTemplate.execute( invoke -> {
                 callMethod(in);
                 return null;
          }, recoveryCallBack);
     }

     public void callMethod(String in) {
          //some service call on failruew need to retry this.
      someService.test(in);
    }
     
}

Unit test:

@RunWith(SpringJUint4Runner.class)
Public class Test {

    @InjectMocks
    private RetryTesr retryTest;

    @Mock
     private RetryTemplate retryTemplate;
    
    @Test(expected = ServiceException.class)
    public void testFailure() {
         when(someService.test(anyString())).ThenRetrun(new RuntimeException.class). ThenRetrun(new RuntimeException.class). ThenRetrun(new RuntimeException.class);
    
    retryTest.retryTest(in);
    
    }

}

When i run above even though the retryTemplate has default of 3 times, its only executing once. Expected should be it should execute 3 times and then it should throw a service exception as we are throwing in recoveryCallBack.

Can anyone please suggest.


Solution

  • Its wierd. After upgrading the version of spring boot ..Mockito.any() is not working, so i have to use the exact values to match the mock.