I have an interface like
public interface WithMD5Calculator{
default String getMd5(){
try{
MessageDigest md = MessageDigest.getInstance("MD5");
//... not important
}catch(NoSuchAlgorithmException e){
//... do some extra stuff and throw wrapped in ServiceException
}
}
// rest of code not important
}
And test that should verify exception handling:
@RunWith(PowerMockRunner.class)
@PrepareForTest(MessageDigest.class)
public class WithMD5Calculator{
@Test
public void shouldHandleNSAEx(){
PowerMockito.mockStatic(MessageDigest.class);
Mockito.when(MessageDigest.getInstance("MD5")).thenThrow(new NoSuchAlgorithmException("Throwed"));
WithMD5Calculator sut = new WithMD5Calculator(){};
ExceptionAssert.assertThat(()-> sut.getMd5())
.shouldThrow(ServiceException.class);
// some more checks
}
}
But ServiceException
were not throwed. Looks like MessageDigest.getInstance
were not mocked.
Any idea?