Junit test drive

public static String generateSaltString() { SecureRandom random = new SecureRandom(); byte[] salt = random.generateSeed(12); return byteToBase64(salt); } 

I wonder how to write a test case using Junit for this method. Any suggestion?

+4
source share
4 answers

First of all, the new SecureRandom () can be potentially very slow, so you might want to cache it. Check out this answer and this one too.

I would reorganize your code as follows

 public class DoRandomStuff { private RandomUtil randomUtil; public DoRandomStuff(RandomUtil randomUtil) { this.randomUtil = randomUtil; } public String generateSaltString() { byte[] salt = randomUtil.generateSeed(12); return byteToBase64(salt); } public String byteToBase64(byte[] salt) { // Do salt generation logic here return null; } 

generateSeed looks like a utility method call, so it can go into the RandomUtil class, which looks like

 class RandomUtil { private SecureRandom random = new SecureRandom(); public byte[] generateSeed(int seed) { return random.generateSeed(seed); } } 

Now your test class for DoRandomStuff will be easy to write. Deploy the ridiculed randomUtil using some testing platform such as Mockito. Make the mockedRandomUtil.generateSeed (int) method a valid fixed number. Your test really checks to see if the byteToBase64 () method does what it should do. You now have a determinate number for your test. You can provide all kinds of numbers that SecureRandom can create in your test class as separate test cases to check the result of byteToBase64 (). As a plus, your random number generation code is now separate from your base64 code.

Hope this helps.

+3
source

I would check if the result is not null and then call the method several times and compare the results to show that each call returns a different value.

But remember: this does not say anything about the quality of randomness of your results!

 @Test public void testGenerateSaltString() { String salt1 = YourClass.generateSaltString(); String salt2 = YourClass.generateSaltString(); String salt3 = YourClass.generateSaltString(); String salt4 = YourClass.generateSaltString(); assertNotNull(salt1); assertNotNull(salt2); assertNotNull(salt3); assertNotNull(salt4); assertNotEqual(salt1, salt2); assertNotEqual(salt1, salt3); assertNotEqual(salt1, salt4); assertNotEqual(salt2, salt3); assertNotEqual(salt2, salt4); assertNotEqual(salt3, salt4); } 

Given GaborSch's comments, I thought of a somewhat fuzzy test implementation, as this is possible - albeit unlikely - two function calls will generate the same salt:

 @Test public void testGenerateSaltString() { String salt1; String salt2; int differenceCount = 0; for(int i = 0; i < 1000; i++ ) { String salt1 = YourClass.generateSaltString(); String salt2 = YourClass.generateSaltString(); // null is still inacceptable assertNotNull(salt1); assertNotNull(salt2); if(!salt1.equalsIgnoreCase(salt2)) { differenceCount++; } } // check if at least at 95% of all tries resultet in different strings // change this value according to your needs assertTrue(differenceCount >= 950); } 
+1
source

I can imagine two tests:

  • Make sure the return is not zero.
  • Be sure to return base64 if you expect it to be base64. Although it doesn’t matter if you expect any random string.
0
source

You can check 3 things:

  • If the result is null or not
  • If the result meets formal requirements (is a Base64 string)
  • If the result is random enough

For example, the following code may work. I generate 100 different random salts and allow a maximum of 1 match between them.

 @Test public void testGenerateSaltString() { Set<String> salts = new HashSet<String>(); int countSame = 0; BASE64Decoder decoder = new BASE64Decoder(); for(int i=0; i<100; i++) { String salt = YourClass.generateSaltString(); assertNotNull(salt); try { decoder.decodeBuffer(encodedBytes); } catch (Exception e) { fail("Not Base64"); } if (salts.contains(salt)) { sameCount++; } salts.add(salt); } assertTrue(countSame <= 1); } 
0
source

All Articles