Link to home
Start Free TrialLog in
Avatar of karpra
karpra

asked on

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

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

Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

Hello karpra,

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

Regards,

mwvisa1
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of karpra
karpra

ASKER

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)

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial