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) { }
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.");
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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(testencyptByt es,enc); below is the error message
AssertEquals succeeds assertEquals(testencyptByt es.length, enc.length );
is the content does not match or is there any other reason?
Thanks in advance.
junit.framework.AssertionF ailedError : expected:<[B@e39a3e> but was:<[B@a39137>
at junit.framework.Assert.fai l(Assert.j ava:47)
at junit.framework.Assert.fai lNotEquals (Assert.ja va:282)
at junit.framework.Assert.ass ertEquals( Assert.jav a:64)
at junit.framework.Assert.ass ertEquals( Assert.jav a:71)
at com.sjm.merlinrx.util.comm on.EncyptD ecryptTest .testEncry pt(EncyptD ecryptTest .java:37)
at sun.reflect.NativeMethodAc cessorImpl .invoke0(N ative Method)
at sun.reflect.NativeMethodAc cessorImpl .invoke(Na tiveMethod AccessorIm pl.java:39 )
at sun.reflect.DelegatingMeth odAccessor Impl.invok e(Delegati ngMethodAc cessorImpl .java:25)
at java.lang.reflect.Method.i nvoke(Meth od.java:59 7)
at junit.framework.TestCase.r unTest(Tes tCase.java :154)
at junit.framework.TestCase.r unBare(Tes tCase.java :127)
at junit.framework.TestResult $1.protect (TestResul t.java:106 )
at junit.framework.TestResult .runProtec ted(TestRe sult.java: 124)
at junit.framework.TestResult .run(TestR esult.java :109)
at junit.framework.TestCase.r un(TestCas e.java:118 )
at junit.framework.TestSuite. runTest(Te stSuite.ja va:208)
at junit.framework.TestSuite. run(TestSu ite.java:2 03)
at org.eclipse.jdt.internal.j unit.runne r.junit3.J Unit3TestR eference.r un(JUnit3T estReferen ce.java:12 8)
at org.eclipse.jdt.internal.j unit.runne r.TestExec ution.run( TestExecut ion.java:3 8)
at org.eclipse.jdt.internal.j unit.runne r.RemoteTe stRunner.r unTests(Re moteTestRu nner.java: 460)
at org.eclipse.jdt.internal.j unit.runne r.RemoteTe stRunner.r unTests(Re moteTestRu nner.java: 673)
at org.eclipse.jdt.internal.j unit.runne r.RemoteTe stRunner.r un(RemoteT estRunner. java:386)
at org.eclipse.jdt.internal.j unit.runne r.RemoteTe stRunner.m ain(Remote TestRunner .java:196)
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(testencyptByt
AssertEquals succeeds assertEquals(testencyptByt
is the content does not match or is there any other reason?
Thanks in advance.
junit.framework.AssertionF
at junit.framework.Assert.fai
at junit.framework.Assert.fai
at junit.framework.Assert.ass
at junit.framework.Assert.ass
at com.sjm.merlinrx.util.comm
at sun.reflect.NativeMethodAc
at sun.reflect.NativeMethodAc
at sun.reflect.DelegatingMeth
at java.lang.reflect.Method.i
at junit.framework.TestCase.r
at junit.framework.TestCase.r
at junit.framework.TestResult
at junit.framework.TestResult
at junit.framework.TestResult
at junit.framework.TestCase.r
at junit.framework.TestSuite.
at junit.framework.TestSuite.
at org.eclipse.jdt.internal.j
at org.eclipse.jdt.internal.j
at org.eclipse.jdt.internal.j
at org.eclipse.jdt.internal.j
at org.eclipse.jdt.internal.j
at org.eclipse.jdt.internal.j
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Here is a nice article on the subject: http://articles.techrepublic.com.com/5100-10878_11-1027676.html#.
Regards,
mwvisa1