We help IT Professionals succeed at work.

How can i write a Junit test case to test encyption and decryption methods of a class

karpra
karpra asked
on
Medium Priority
2,155 Views
Last Modified: 2013-12-13
How can i write a Junit test case to test encyption and decryption methods of a class where the methods take and return binary array of data. please let me know if you need any further information. Thanks in advance.
encrypt method
public static byte[] encrypt(byte[] str) { }
decrypt method 
 public static byte[] decrypt(byte[] str) { }

Open in new window

Comment
Watch Question

Kevin CrossChief Technology Officer
CERTIFIED EXPERT
Most Valuable Expert 2011

Commented:
Hello karpra,

Here is a nice article on the subject: http://articles.techrepublic.com.com/5100-10878_11-1027676.html#.

Regards,

mwvisa1
Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
basically same way you write any unit test

pass it a byte array that you know the expected result and then compare the returned byte aarray with the expected result array.
We often us a file in these cases to hold the test data, one file for input and another for the expected result. Load the files into a byte array and run the test.
http://helpdesk.objects.com.au/java/how-do-i-read-the-contents-of-a-file-into-a-byte-array

You could also use something like a base64 encoded string in your code instead of a file. Using that you would first convert the string into a byte array before passing it to your method
http://helpdesk.objects.com.au/java/how-do-i-decode-a-base64-encoded-string-using-java
http://helpdesk.objects.com.au/java/how-to-create-a-base64-encoded-string-using-java

As there is some symettry in those two methods you can also pass the result from one into the other and check that what you get back is what you started with.

let me know if you need anymore information.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Kevin CrossChief Technology Officer
CERTIFIED EXPERT
Most Valuable Expert 2011

Commented:
Here is an example using JUnit 4.x.  EncryptDecrypt should be replaced with the actual name of your class with encrypt and decrypt methods.
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*; 
public class EncryptDecryptTest { 
    public EncryptDecryptTest() {
    } 
    @BeforeClass
    public static void setUpClass() throws Exception {
    } 
    @AfterClass
    public static void tearDownClass() throws Exception {
    } 
    @Before
    public void setUp() {
    } 
    @After
    public void tearDown() {
    } 
    /**
     * Test of encrypt method, of class EncryptDecrypt.
     */
    @Test
    public void testEncrypt() {
        System.out.println("encrypt");
        byte[] str = null;
        byte[] expResult = null;
        byte[] result = EncryptDecrypt.encrypt(str);
        assertEquals(expResult, result);
        // TODO review the generated test code and remove the default call to fail.
        fail("The test case is a prototype.");
    } 
    /**
     * Test of decrypt method, of class EncryptDecrypt.
     */
    @Test
    public void testDecrypt() {
        System.out.println("decrypt");
        byte[] str = null;
        byte[] expResult = null;
        byte[] result = EncryptDecrypt.decrypt(str);
        assertEquals(expResult, result);
        // TODO review the generated test code and remove the default call to fail.
        fail("The test case is a prototype.");
    } 
}

Open in new window

Kevin CrossChief Technology Officer
CERTIFIED EXPERT
Most Valuable Expert 2011
Commented:
With objects suggestion, you could do in one test.  See objects links for good testing implementation using the files also.
public void testEncryptAndDecrypt() {
        System.out.println("encrypt");
        byte[] str = null;
        byte[] result = EncryptDecrypt.decrypt(EncryptDecrypt.encrypt(str));
        assertEquals(str, result);
        // TODO review the generated test code and remove the default call to fail.
        fail("The test case is a prototype.");
    } 

Open in new window

Author

Commented:
Thanks guru ,
sorry i was on vacation could not respond.
I have tested for encryption and got the below message but when i compare with binary data size its true and test succeeds. but when compared with actual binary array it fails.

Assert Failes with byte array -- assertEquals(testencyptBytes,enc); below is the error message
AssertEquals succeeds assertEquals(testencyptBytes.length,enc.length);
is the content does not match or is there any other reason?
Thanks in advance.

junit.framework.AssertionFailedError: expected:<[B@e39a3e> but was:<[B@a39137>
      at junit.framework.Assert.fail(Assert.java:47)
      at junit.framework.Assert.failNotEquals(Assert.java:282)
      at junit.framework.Assert.assertEquals(Assert.java:64)
      at junit.framework.Assert.assertEquals(Assert.java:71)
      at com.sjm.merlinrx.util.common.EncyptDecryptTest.testEncrypt(EncyptDecryptTest.java:37)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at junit.framework.TestCase.runTest(TestCase.java:154)
      at junit.framework.TestCase.runBare(TestCase.java:127)
      at junit.framework.TestResult$1.protect(TestResult.java:106)
      at junit.framework.TestResult.runProtected(TestResult.java:124)
      at junit.framework.TestResult.run(TestResult.java:109)
      at junit.framework.TestCase.run(TestCase.java:118)
      at junit.framework.TestSuite.runTest(TestSuite.java:208)
      at junit.framework.TestSuite.run(TestSuite.java:203)
      at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
      at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
you can't use assertEquals on the array, you need to instead compare each element in the array individually in a loop

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.