1515import org .junit .Test ;
1616import java .util .Base64 ;
1717
18- public class EncryptionDecryptionUtilTest {
18+ public class EncryptionDecryptionUtilsTest {
1919
2020 @ Test
2121 public void testEncryptDecrypt () {
@@ -36,25 +36,22 @@ public void testDecryptingWithWrongKey() {
3636
3737 String encryptedString = EncryptionDecryptionUtil .encrypt (secret1 , data );
3838
39- try {
40- EncryptionDecryptionUtil .decrypt (secret2 , encryptedString );
41- Assert .fail ("Should have thrown an exception when decrypting with a wrong key" );
42- } catch (RuntimeException e ) {
43- // Expected exception
44- }
39+ RuntimeException ex = Assert .assertThrows (RuntimeException .class , () -> EncryptionDecryptionUtil .decrypt (secret2 , encryptedString ));
40+
41+ Assert .assertEquals ("The cipher was unable to perform pass over data" , ex .getMessage ());
4542 }
4643
4744 @ Test
4845 public void testDecryptingCorruptedData () {
4946 String secret = Base64 .getEncoder ().encodeToString ("mySecretKey12345" .getBytes ());
5047 String corruptedEncryptedString = "corruptedData" ;
5148
52- try {
53- EncryptionDecryptionUtil . decrypt ( secret , corruptedEncryptedString );
54- Assert . fail ( "Should have thrown an exception when trying to decrypt corrupted data" );
55- } catch ( RuntimeException e ) {
56- // Expected exception
57- }
49+ RuntimeException ex = Assert . assertThrows (
50+ RuntimeException . class ,
51+ () -> EncryptionDecryptionUtil . decrypt ( secret , corruptedEncryptedString )
52+ );
53+
54+ Assert . assertEquals ( "Last unit does not have enough valid bits" , ex . getMessage ());
5855 }
5956
6057 @ Test
@@ -69,7 +66,7 @@ public void testEncryptDecryptEmptyString() {
6966 }
7067
7168 @ Test (expected = NullPointerException .class )
72- public void testEncryptDecryptNullValue () {
69+ public void testEncryptNullValue () {
7370 String secret = Base64 .getEncoder ().encodeToString ("mySecretKey12345" .getBytes ());
7471 String data = null ;
7572
0 commit comments