mercredi 7 novembre 2018

Testing try-catch blocks in Java

Is there any viable way to test the try-catch blocks, found in the below Java method, without changing its current implementation.

public String methodToTest(String text) {
    try
    {
        if (text.contains("something")) {
            throw new CustomException();
        }

        final MyObj myObj = new MyObj(text); //this can throw a MyObjException

        return text;

    } catch (CustomException e) {
        return "An exception has been thrown";
    } catch (MyObjException e) {
        return "An exception has been thrown";
    } catch (Exception e) {
        return "An exception has been thrown";
    }
}

Aucun commentaire:

Enregistrer un commentaire