Below is my test code.
import org.junit.Test;
import org.mockito.Mockito;
public class classA {
    @Mock
    EncSercice encService;
    @InjectMocks
    ServiceClass service;
    @BeforeEach
    public void setUp() {
      MockitoAnnotations.initMocks(this);
    }
    @Test
    public void testEncryptRegisterTransactionPayload() {
        
        ValidRequest request = new ValidRequest();
        try {
            
            Mockito.when(encService.callFn(Mockito.anyString(), Mockito.any(String[].class)))
                    .thenReturn("Stubbed encrypted message");
            service.sendPayload(request);
            Mockito.verify(encService).callFn(Mockito.anyString(), Mockito.any(String[].class));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Here I'm trying to mock/stub the encService.callFn method. But when i run it, It's actually calling method. I can see the logs which I have mentioned in callFn method. Even i have tried using doReturn().when(), Still it's calling the actual method. How can I solve this?
EncService class:
class EncSercice implements EncSerciceImp {
    public String callFn(final String req, final String[] keys) throws Exception {
    byte[] output = // calling a private method and passing req and keys as args....
    return new String(output);
}
}
// ServiceClass  code 
public void sendPayload(final ValidRequest  req) {
    // some code imp..
    EncSercice encService = new EncSercice();
    String str = encService.callFn(req, keys);
}
note: callFn will return a dynamic string.
 
    