java - Unit testing of encrypt/decrypt -
i have implemented simple class called enigma
has symmetric key , 2 methods: byte[] encryptstring(string strtoencrypt)
, string decryptbytes(byte[] arraytodecrypt)
.
i trying write tests methods. have thought of testing encrypt , decrypt methods inverse of each other, says nil each of them individually. wanted utilize methods obtain battery of input-outputs , set tests (i know not ideal, purpose of these tests guarantee function behaves in future today, not encryption/decryption good).
however, not know how obtain representation of byte array output byte[] encryptstring(string strtoencrypt)
can hardcode in test class.
any ideas?
a few notes on how test ( personal sentiment warning :-) )
if you're writing unit tests, avoid reading expected results files, since slows downwards test (unit tests have super fast), create thing irrelevant code might go wrong (i.e. file might deleted, etc.) don't utilize same method you're testing created expected results check method. pointless if you're trying check algorithm, , might perpetuate bugs in algorithm consider fact smallest unit of work in java class, not method (forget sec java 8's lambda expressions, won't write unit tests directly), therefore, should effort test class, not methodsmy lastly point there brings me (finally ?) recommendation. think what's responsibility of class (hopefully, single responsibility, see srp). in case, believe responsibility of class 2 way encryption of strings. therefore, write next tests:
@test public void testthatencryptingstringresultsinexpectedbytes() { byte[] encryption = enigma.encryptstring(test_string); assertarrayequals(expected_encryption, encryption); } @test public void testthatdecryptinencryptionresultsinoriginalstring() { string decryption = enigma.decryptbytes(test_encryption); assertequals(expected_original_string, decryption); } @test public void testthatdecriptionreversesencryption() { string decryption = enigma.decryptbytes(enigma.encryptstring(test_string)); assertequals(test_string, decryption); } @test public void testthatencryptionreversesdecryption() { byte[] encryption = enigma.encriptstring(enigma.decryptbytes(test_encryption)); assertequals(test_encryption, encryption); }
maybe add together more tests check trying encrypt/decrypt invalid values throws exceptions, , other error cases.
java unit-testing junit bytearray junit4
No comments:
Post a Comment