Search code examples
javatestingwebsphere-libertytestcontainersmicroprofile

How can I test the @Retry and @Fallback annotations in OpenLiberty?


I have the next methods in my service class that try to send an email. If my email client throws a RuntimeException, it will make 4 retries to call the email client, and send the email. In case of an error after the 4th retry it will revert to the sendEmailFallback method.

@Retry(maxRetries = 4, delay = 1000, jitter = 200, retryOn = RuntimeException.class)
@Fallback(fallbackMethod = "sendEmailFallback")
public String sendEmailNow(EmailData data) {
    client.sendEmail(data);
    return "Email sent successfully!";
}

public String sendEmailFallback(EmailData data) {
    LOGGER.error("Failed to send email after retries. Executing fallback method.");
    return "Failed to send email after retries. Executing fallback method.";
}

How can I test this?

Currently, my application uses test containers for end to end testing. I am loading all the necessary configuration files to my open liberty container to execute the tests that assert a 200 status code. Once the test containers are up, I cannot modify any configuration file to simulate an error. I know that I could create a second instance of the open liberty container that loads the files necessary to make my application fail, but I want to avoid that kind of implementation. Do you have any suggestions?


Solution

  • Test containers has a module called Toxiproxy that should be able to help with this. https://java.testcontainers.org/modules/toxiproxy/

    You can add it as a proxy between your services and use it to simulate network issues like latency or timeouts between your services.