Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking MessageDigest.getInstance() to throw an exception

I got the following method:

private MessageDigest getMessageDigest() {
    try {
        return MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        throw new Error(e);
    }
}

To get 100% code coverage I need to get into the catch block. But I am absolutely not sure how I can do that. Is there some mocking framework that could help me in this case? If so - how? Or is there even another way without having to catch an exception?

like image 891
tyrondis Avatar asked May 18 '11 23:05

tyrondis


1 Answers

The getInstance method on MessageDigest looks like a static method. Static methods cannot be mocked. I agree with ratchet that you should not aim for 100 % code coverage but focus on testing the areas with complex code instead.

like image 110
ThomasArdal Avatar answered Sep 26 '22 15:09

ThomasArdal