HashUtils { @NonNull public static String sha256(@NonNull String data) { return sha256(data, "UTF-8"); } @VisibleForTesting  HashUtils(); @NonNull static String sha256(@NonNull String data);  }
@Test public void sha256() { assertEquals("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", HashUtils.sha256("")); assertEquals("7efd873c874fbf92d6c3eccc2f24f7eaa349d9d7b512d81ff3f1b44e896362fb", HashUtils.sha256("This hash function rocks!")); }
@Test(expected = RuntimeException.class) @PrepareForTest(HashUtils.class) public void algorithmNotFound() throws NoSuchAlgorithmException { mockStatic(MessageDigest.class); NoSuchAlgorithmException cause = new NoSuchAlgorithmException(); doThrow(cause).when(MessageDigest.class); MessageDigest.getInstance(anyString()); try { HashUtils.sha256(""); } catch (RuntimeException e) { assertEquals(cause, e.getCause()); throw e; } }
@Test(expected = RuntimeException.class) public void utf8NotFound() { try { HashUtils.sha256("", "Some Invalid Encoding"); } catch (RuntimeException e) { assertTrue(e.getCause() instanceof UnsupportedEncodingException); throw e; } }